postgresql-9.4

Merging Concatenating JSON(B) columns in query

非 Y 不嫁゛ 提交于 2019-11-30 03:13:50
Using Postgres 9.4, I am looking for a way to merge two (or more) json or jsonb columns in a query. Consider the following table as an example: id | json1 | json2 ---------------------------------------- 1 | {'a':'b'} | {'c':'d'} 2 | {'a1':'b2'} | {'f':{'g' : 'h'}} Is it possible to have the query return the following: id | json ---------------------------------------- 1 | {'a':'b', 'c':'d'} 2 | {'a1':'b2', 'f':{'g' : 'h'}} Unfortunately, I can't define a function as described here . Is this possible with a "traditional" query? Here is the complete list of build-in functions that can be used

Incorrect sort/collation/order with spaces in Postgresql 9.4

只愿长相守 提交于 2019-11-29 15:12:35
I'm using Postgresql 9.4.5. When I go to psql and run \l I get Encoding is UTF8 Collate is en_US.UTF-8 cCtype is en_US.UTF-8 I have products table with a name column that has the following names: T-700A Grouped T-700 AGrouped T-700A Halved T-700 Whole When I execute the following SQL in pql SELECT name FROM products WHERE name LIKE '%T-700%' ORDER By name ASC; I get the following output T-700A Grouped T-700 AGrouped T-700A Halved T-700 Whole That sorting doesn't look natural. I expected to get T-700 AGrouped T-700 Whole T-700A Grouped T-700A Halved It doesn't seem like Postgres is handling

postgres change jsonb[] to jsonb

偶尔善良 提交于 2019-11-29 13:07:58
I use postgres9.4 , and there exists relation "Patients" has column "contact" with type jsonb[] , how to transfer type jsonb[] to jsonb ? The following is on record. =>select name, contact from "Patients" where contact is not null; name | contact --------+----------------------------------------------------------------------------------------------------- "tom" | {"{\"name\": \"tom\", \"phone\": \"111111\", \"address\": \"shanghai\", \"relation\": \"your_relation\"}"} I have tried as the followings, contact4 is column with type jsonb alter table "Patients" alter column contact4 type jsonb

How to convert PostgreSQL 9.4's jsonb type to float

Deadly 提交于 2019-11-29 04:50:44
问题 I'm trying the following query: SELECT (json_data->'position'->'lat') + 1.0 AS lat FROM updates LIMIT 5; (The +1.0 is just there to force conversion to float. My actual queries are far more complex, this query is just a test case for the problem.) I get the error: ERROR: operator does not exist: jsonb + numeric If I add in explicit casting: SELECT (json_data->'position'->'lat')::float + 1.0 AS lat FROM updates LIMIT 5; the error becomes: ERROR: operator does not exist: jsonb + double

Appending (pushing) and removing from a JSON array in PostgreSQL 9.2, 9.3, and 9.4?

徘徊边缘 提交于 2019-11-29 02:16:37
For versions greater than 9.5 see this question I have created a table in PostgreSQL using this: CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2, '["japan","china","india", "russia", "australia"]'), (3, '["must", "match"]'), (4, '["abra","value","true", "apple", "sample"]'), (5, '["abra","false","mango", "apple", "sample"]'), (6, '["string","value","mango", "apple", "sample"]'), (7, '["must", "watch"]') ) AS t(id,jsondata); Now what I wanted was to add Something like append_to_json_array takes in the actual

user postgres launches process that takes all CPUs 100% usage

一笑奈何 提交于 2019-11-28 12:43:00
问题 User postgres is running a process that take all CPUs at 100% usage in a centos machine, the postgresql service is not running so it cannot be a query. When I try to stop the process it restarts itself. Then name of the process is somewhat strange. 回答1: Congratulations! By exposing a database with a weak superuser password to the internet you invited somebody to break in and use your CPU for their own purposes, probably mining crypto-currencies. Take the machine from the internet, wipe it

How do I return a jsonb array and array of objects from my data?

北战南征 提交于 2019-11-28 10:08:23
问题 I have the following table: CREATE TABLE mytable ( id serial PRIMARY KEY , employee text UNIQUE NOT NULL , data jsonb ); With the following data: INSERT INTO mytable (employee, data) VALUES ('Jim', '{"sales_tv": [{"value": 10, "yr": "2010", "loc": "us"}, {"value": 5, "yr": "2011", "loc": "europe"}, {"value": 40, "yr": "2012", "loc": "asia"}], "sales_radio": [{"value": 11, "yr": "2010", "loc": "us"}, {"value": 8, "yr": "2011", "loc": "china"}, {"value": 76, "yr": "2012", "loc": "us"}],

Incorrect sort/collation/order with spaces in Postgresql 9.4

主宰稳场 提交于 2019-11-28 09:18:42
问题 I'm using Postgresql 9.4.5. When I go to psql and run \l I get Encoding is UTF8 Collate is en_US.UTF-8 cCtype is en_US.UTF-8 I have products table with a name column that has the following names: T-700A Grouped T-700 AGrouped T-700A Halved T-700 Whole When I execute the following SQL in pql SELECT name FROM products WHERE name LIKE '%T-700%' ORDER By name ASC; I get the following output T-700A Grouped T-700 AGrouped T-700A Halved T-700 Whole That sorting doesn't look natural. I expected to

Referencing current row in FILTER clause of window function

為{幸葍}努か 提交于 2019-11-28 09:09:27
In PostgreSQL 9.4 the window functions have the new option of a FILTER to select a sub-set of the window frame for processing. The documentation mentions it, but provides no sample. An online search yields some samples, including from 2ndQuadrant but all that I found were rather trivial examples with constant expressions. What I am looking for is a filter expression that includes the value of the current row. Assume I have a table with a bunch of columns, one of which is of date type: col1 | col2 | dt ------------------------ 1 | a | 2015-07-01 2 | b | 2015-07-03 3 | c | 2015-07-10 4 | d |

postgres change jsonb[] to jsonb

流过昼夜 提交于 2019-11-28 07:03:41
问题 I use postgres9.4 , and there exists relation "Patients" has column "contact" with type jsonb[] , how to transfer type jsonb[] to jsonb ? The following is on record. =>select name, contact from "Patients" where contact is not null; name | contact --------+----------------------------------------------------------------------------------------------------- "tom" | {"{\"name\": \"tom\", \"phone\": \"111111\", \"address\": \"shanghai\", \"relation\": \"your_relation\"}"} I have tried as the