postgresql-9.1

PostgreSQL: joining arrays within group by clause

纵饮孤独 提交于 2019-12-04 05:55:05
We have a problem grouping arrays into a single array. We want to join the values from two colums into one single array and aggregate these arrays of multiple rows. Given the following input: | id | name | col_1 | col_2 | | 1 | a | 1 | 2 | | 2 | a | 3 | 4 | | 4 | b | 7 | 8 | | 3 | b | 5 | 6 | We want the following output: | a | { 1, 2, 3, 4 } | | b | { 5, 6, 7, 8 } | The order of the elements is important and should correlate with the id of the aggregated rows. We tried the array_agg function: SELECT array_agg(ARRAY[col_1, col_2]) FROM mytable GROUP BY name; Unfortunately, this statement

Create array in SELECT

爱⌒轻易说出口 提交于 2019-12-04 05:39:52
I'm using PostgreSQL 9.1 and I have this data structure: A B ------- 1 a 1 a 1 b 1 c 1 c 1 c 1 d 2 e 2 e I need a query that produces this result: 1 4 {{c,3},{a,2},{b,1},{d,1}} 2 1 {{e,2}} A=1, 4 rows total with A=1, the partial counts (3 rows with c value, 2 rows with a value, .....) The distinct values of column "A" The count of all rows related to the "A" value An array contains all the elements related to the "A" value and the relative count of itself The sort needed for the array is based of the count of each group (like the example 3,2,1,1). This should do the trick: SELECT a , sum(ab_ct

How do I change the default client_encoding in Postgres?

六眼飞鱼酱① 提交于 2019-12-04 04:01:37
I'm trying to change the default value for the client_encoding configuration variable for a PostgreSQL database I'm running. I want it to be UTF8 , but currently it's getting set to LATIN1 . The database is already set to use UTF8 encoding: application_database=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ----------------------+----------+----------+-------------+-------------+-------------------------------------- postgres | postgres | LATIN1 | en_US | en_US | application_database | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres + |

Postgres function returning one record while I have many records?

元气小坏坏 提交于 2019-12-04 03:54:38
问题 I have many records which my simple query returning but when i use function it just gives me first record, firstly i create my own data type using, CREATE TYPE my_type (usr_id integer , name varchar(30)); and my function is, CREATE OR REPLACE function test() returns my_type as $$ declare rd varchar := '21'; declare personphone varchar := NULL; declare result my_type; declare SQL VARCHAR(300):=null; DECLARE radiophone_clause text = ''; BEGIN IF rd IS NOT NULL then radiophone_clause = 'and pp

PostgreSQL date difference

吃可爱长大的小学妹 提交于 2019-12-04 00:40:14
问题 I have a PostgreSQL function which calculates date difference: CREATE OR REPLACE FUNCTION testDateDiff () RETURNS int AS $BODY$ DECLARE startDate TIMESTAMP; DECLARE endDate TIMESTAMP; DECLARE diffDatePart int ; BEGIN Select evt_start_date From events Where evt_id = 5 INTO startDate ; Select evt_start_date From events Where evt_id = 6 INTO endDate ; SELECT EXTRACT(day FROM TIMESTAMP startDate - endDate) INTO diffDatePart; RETURN diffDatePart; END; $BODY$ LANGUAGE plpgsql COST 100 If dates are

Is there data visualisation tool for postgresql which is capable of displaying inter schema relations as well? [closed]

岁酱吖の 提交于 2019-12-03 22:14:31
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Operating system used is linux. I have tried Navicat and SQL Power Architect . They did display relations between tables in the same schema. I have some foreign key constraints which reference tables in a different schema. Am I missing something with respect to Navicat and PostgreSQL Maestro? Can they not display the inter schema relations? Is there data visualisation tool for postgresql which is

Error “invalid byte sequence” while restoring PostgreSQL database

泄露秘密 提交于 2019-12-03 20:29:24
Earlier today, I was trying to restore my PostgreSQL (8.1.22) database from production using pgAdmin III. However, after the restoration procedure finished, it started throwing errors like: WARNING: errors ignored on restore: 4 Also, upon investigation I found that out of all the tables 3 tables hadn't been restored (contained 0 rows). When I checked the log, I found the follwoing error near the 3 tables: pg_restore: [archiver (db)] Error from TOC entry 5390; 0 442375 TABLE DATA tablename postgres pg_restore: [archiver (db)] COPY failed: ERROR: invalid byte sequence for encoding "UTF8":

cannot create extension without superuser role

牧云@^-^@ 提交于 2019-12-03 18:32:42
问题 I'm trying to run unit tests in Django, and it creates a new database. The database has postgis extensions and when I regularly create the database, I use "CREATE ExTENSION postgis". However, when I run tests, it gives me the following error: $ ./manage.py test Creating test database for alias 'default'... Got an error creating the test database: database "test_project" already exists Type 'yes' if you would like to try deleting the test database 'test_project', or 'no' to cancel: yes

List constraints for all tables with different owners in PostgreSQL

自古美人都是妖i 提交于 2019-12-03 17:22:36
问题 Do I have to be owner of relation to access constraint related data in information schema? I've tested the following and it seems that I have to be the owner. create schema rights_test; create table rights_test.t1 (id int primary key); create table rights_test.t2 (id int references rights_test.t1(id)); select tc.constraint_name, tc.constraint_schema || '.' || tc.table_name || '.' || kcu.column_name as physical_full_name, tc.constraint_schema, tc.table_name, kcu.column_name, ccu.table_name as

Spring Security JDBC authentication default schema error when using PostgreSQL

£可爱£侵袭症+ 提交于 2019-12-03 16:39:17
问题 Is it really impossible to use default schema for Spring Security with PostgreSQL, because the part "varchar_ignorecase" does not exist can't be replaced? I'm just testing the default settings: auth.jdbcAuthentication() .dataSource(dataSource) .withDefaultSchema(); And below is the error: Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.servlet.Filter org.springframework.security.config.annotation.web.configuration