postgresql-9.1

Aggregate hstore column in PostreSQL

跟風遠走 提交于 2019-11-29 14:37:14
问题 I have a table like this: Table "public.statistics" id | integer | not null default nextval('statistics_id_seq'::regclass) goals | hstore | items: |id |goals | |30059 |"3"=>"123" | |27333 |"3"=>"200", "5"=>"10" | What I need to do for aggregate all values by key in hash? I want to get result like this: select sum(goals) from statistics return |goals | |"3"=>"323", "5"=>"10" | 回答1: Building on Laurence's answer, here's a pure SQL way to aggregate the summed key/value pairs into a new hstore

Can the Postgres data type NUMERIC store signed values?

我的未来我决定 提交于 2019-11-29 13:44:46
In PostgreSQL, I would like to store signed values -999.9 - 9999.9 . Can I use numeric(5.1) for this? Or what type should I use? Erwin Brandstetter You can certainly use the arbitrary precision type numeric with a precision of 5 and a scale of 1, just like @Simon commented , but without the syntax error. Use a comma( , ) instead of the dot ( . ) in the type modifier: SELECT numeric(5,1) '-999.9' AS nr_lower , numeric(5,1) '9999.9' AS nr_upper; nr_lower | nr_upper ----------+---------- -999.9 | 9999.9 The minus sign and the dot in the string literal do not count against the allowed maximum of

Mountain Lion Postgres could not connect

我只是一个虾纸丫 提交于 2019-11-29 09:30:21
After my update to mountain lion my postgres doest work. It is still running but my applications cant connect to it anymore. $ ps aux | grep postgres postgres 204 0.0 0.0 2446960 836 ?? Ss 7:31AM 0:00.59 postgres: stats collector process postgres 203 0.0 0.1 2478732 2240 ?? Ss 7:31AM 0:00.41 postgres: autovacuum launcher process postgres 202 0.0 0.0 2478600 584 ?? Ss 7:31AM 0:00.34 postgres: wal writer process postgres 201 0.0 0.0 2478600 784 ?? Ss 7:31AM 0:00.48 postgres: writer process postgres 95 0.0 0.0 2446960 368 ?? Ss 7:31AM 0:00.11 postgres: logger process postgres 64 0.0 0.2 2478600

Printing to screen in .sql file postgres

孤者浪人 提交于 2019-11-29 09:04:01
This sounds like it should be a very easy thing to do, however, I cannot find ANYWHERE how to do it. I have a .sql file I am building for an upgrade to my application that alters tables, inserts/updates, etc. I want to write to the screen after every command finishes. So, for instance if I have something like: insert into X... I want to see something like, Starting to insert into table X Finished inserting into table X Is this possible in Postgres? If you're just feeding a big pile of SQL to psql then you have a couple options. You could run psql with --echo-all : -a --echo-all Print all input

FATAL: password authentication failed for user “postgres”

独自空忆成欢 提交于 2019-11-29 08:59:37
问题 getting this error message in ubuntu. in pg_hba.conf file, I tried using 'ident','peer','trust','md5' in different times.but no go. please help. 回答1: in your pg_hba.conf # IPv4 local connections: # TYPE DATABASE USER CIDR-ADDRESS METHOD host all all 127.0.0.1/32 trust if it does not work then try with host all all your_ip/32 trust then restart your data base it will work fine 回答2: If the username and password are correct then md5 is the correct value. Make sure you restart the database

Alphanumeric case in-sensitive sorting in postgres

 ̄綄美尐妖づ 提交于 2019-11-29 05:35:12
I am new to postrges and want to sort varchar type columns. want to explain the problem with with below example: table name: testsorting order name 1 b 2 B 3 a 4 a1 5 a11 6 a2 7 a20 8 A 9 a19 case sensitive sorting (which is default in postgres) gives: select name from testsorting order by name; A B a a1 a11 a19 a2 a20 b case in-sensitive sorting gives: select name from testsorting order by UPPER(name); A a a1 a11 a19 a2 a20 B b how can i make alphanumeric case in-sensitive sorting in postgres to get below order : a A a1 a2 a11 a19 a20 b B I wont mind the order for capital or small letters,

PostgreSQL with-delete “relation does not exists”

有些话、适合烂在心里 提交于 2019-11-29 04:10:16
I am using postgreSQL 9.1 and I want to delete duplicates from my table using this tip: https://stackoverflow.com/a/3822833/2239537 So, my query looks like that: WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY code, card_id, parent_id ORDER BY id DESC) RN FROM card) DELETE FROM cte WHERE RN > 1 But it shows me ERROR: relation "cte" does not exist SQL state: 42P01 Character: 157 However this statement works fine: WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY code, card_id, parent_id ORDER BY id DESC) RN FROM merchantcard) SELECT * FROM cte WHERE RN > 1 Any ideas how to get it work?

Get a timestamp from concatenating day and time columns

一个人想着一个人 提交于 2019-11-29 03:38:21
I am having day and time fields in database. I want to get the time-stamp by concatenating the day and time. How to do this in PostgreSQL? I have done this: SELECT EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28'); And it is working fine. But when I tried replacing day and time fields I am getting the following error: SELECT EXTRACT(EPOCH FROM TIMESTAMP Day || ' ' || Time); ERROR: syntax error at or near "Day" LINE 1: ...quest_count > 0 AND (EXTRACT(EPOCH FROM TIMESTAMP Day || ' ' || Time)) > (e... SELECT EXTRACT(EPOCH FROM (Day || ' ' || Time)::timestamp); date and time types If your Day is

SELECT .. INTO to create a table in PL/pgSQL

浪子不回头ぞ 提交于 2019-11-29 02:53:39
I want to use SELECT INTO to make a temporary table in one of my functions. SELECT INTO works in SQL but not PL/pgSQL. This statement creates a table called mytable (If orig_table exists as a relation): SELECT * INTO TEMP TABLE mytable FROM orig_table; But put this function into PostgreSQL, and you get the error: ERROR: "temp" is not a known variable CREATE OR REPLACE FUNCTION whatever() RETURNS void AS $$ BEGIN SELECT * INTO TEMP TABLE mytable FROM orig_table; END; $$ LANGUAGE plpgsql; I can SELECT INTO a variable of type record within PL/pgSQL, but then I have to define the structure when

How are import statements in plpython handled?

一笑奈何 提交于 2019-11-29 02:25:12
问题 I have a plypython function which does some json magic. For this it obviously imports the json library. Is the import called on every call to the function? Are there any performance implication I have to be aware of? 回答1: The import is executed on every function call. This is the same behavior you would get if you wrote a normal Python module with the import statement inside a function body as oppposed to at the module level. Yes, this will affect performance. You can work around this by