postgresql-9.4

Create nested json from sql query postgres 9.4

a 夏天 提交于 2019-12-05 07:41:17
I need to get as a result from query fully structured JSON. I can see in postgres that there are some built in functions that may be useful. As an example I created a structure as follows: -- Table: person -- DROP TABLE person; CREATE TABLE person ( id integer NOT NULL, name character varying(30), CONSTRAINT person_pk PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); ALTER TABLE person OWNER TO postgres; -- Table: car -- DROP TABLE car; CREATE TABLE car ( id integer NOT NULL, type character varying(30), personid integer, CONSTRAINT car_pk PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); ALTER TABLE car OWNER TO

Correct syntax for array of composite type

泪湿孤枕 提交于 2019-12-05 04:53:56
问题 CREATE TYPE pencil_count AS( pencil_color varchar(30), count integer ); CREATE TABLE pencils(id serial, pencils_ pencil_count[]); INSERT INTO pencils(pencils_) VALUES('{("blue",5),("red",2)}'); This doesn't work and gives error: Malformed array literal. What would be the correct syntax if I want to add this composite array without using ARRAY[...] ? 回答1: I want to add this composite array without using ARRAY You could use: INSERT INTO pencils(pencils_) VALUES('{"(\"blue\",5)","(\"red\",2)"}')

Roughly how fast is JSON -> JSONB column conversion in Postgres 9.4

梦想的初衷 提交于 2019-12-05 04:05:56
I'm looking to migrate from Postgres 9.3 to 9.4, and have a lot of data in JSON columns. While it's fine, I wanted to have a look at migrating to the more efficient column storage (which JSONB seems to be — a really exciting piece of tech!). To actually migrate, I want to know migration characteristics for something like ALTER TABLE table_with_json ALTER COLUMN my_json SET DATA TYPE jsonb USING my_json::jsonb; (from this helpful question ). Ideally, it would be good to know how long it takes to migrate 1mil and 10mil entries, and how it scales. While I can get these numbers myself, I thought

Difference between set, \\set and \\pset in psql

纵饮孤独 提交于 2019-12-05 01:29:42
I get a little confused some times when working with psql between when to use a set vs. \set vs. \pset . I think that: set is for session variables on my connection to the db. For example SET ROLE dba ; \set is for local variables for this psql session. For example \set time 'select current_timestamp' \pset is for psql settings for this psql session. For example '\pset border 2' But, I've never found what I thought was a good explanation of each. Are my assumptions above correct? I'm using PostgreSQL 9.4 Basically correct. The important difference is that SET is an SQL command while the other

Query and order by number of matches in JSON array

♀尐吖头ヾ 提交于 2019-12-05 00:34:39
Using JSON arrays in a jsonb column in Postgres 9.4 and Rails, I can set up a scope that returns all rows containing any elements from an array passed to the scope method - like so: scope :tagged, ->(tags) { where(["data->'tags' ?| ARRAY[:tags]", { tags: tags }]) } I'd also like to order the results based on the number of matched elements in the array. I appreciate I might need to step outside the confines of ActiveRecord to do this, so a vanilla Postgres SQL answer is helpful too, but bonus points if it can be wrapped up in ActiveRecord so it can be a chain-able scope. As requested, here's an

Join tables using a value inside a JSONB column

一曲冷凌霜 提交于 2019-12-04 17:18:11
There are two tables: Authorized Contacts ( auth_contacts ): ( userid varchar contacts jsonb ) contacts contains an array of contacts with attributes {contact_id, type} discussion : ( contact_id varchar discussion_id varchar discussion_details jsonb ) The table auth_contacts has at least 100k records making it non JSONB type is not appropriate according as it would double or triple the amount of records. Sample data for auth_contacts : userid | contacts '11111' | '{"contact": [{"type": "type_a", "contact_id": "1-A-12"} , {"type": "type_b", "contact_id": "1-A-13"}]}' discussion table has 5

Can we define a GROUP_CONCAT function in PostgreSQL?

本小妞迷上赌 提交于 2019-12-04 07:08:30
问题 I have several lines of SQL code for my legacy database with GROUP_CONCAT statements, as in: SELECT SUM(age), GROUP_CONCAT(sal) FROM Users; In PostgreSQL, I can do the same with: SELECT SUM(age), string_agg(sal, ', ') FROM Users; I would like to reuse the old SQL as much as possible. So I need to define a GROUP_CONCAT function that internally calls string_agg . Is this possible? EDIT: The linked question is unrelated! My question asks "How to define a function called group_concat?". The

Postgres function returning a row as JSON value

 ̄綄美尐妖づ 提交于 2019-12-04 04:38:29
问题 I am pretty new to PG, and trying to convert from MSSQL. I am working on a function that will return JSON results. This one works: Create Or Replace Function ExampleTable_SelectList() Returns JSON As $$ Select array_to_json(array_agg(row_to_json(t))) From (Select id, value1, value2, From ExampleTable) t $$ Language SQL; Now, I want to call can update that returns a value and turn that value into JSON to return. So, this one gives an error on the set command. Create Or Replace Function

Revoke access to postgres database for a role

风流意气都作罢 提交于 2019-12-03 15:53:48
I have created a separate role "newrole" and new schema "newschema" for a certain user that should only execute some stored functions. I have managed to revoke access to schema "public" for the current database. Logged in as "newrole" I still have access to postgres database like this: SELECT * FROM pg_user I want to revoke all access to the postgres database and tried following that not work: REVOKE ALL ON DATABASE postgres FROM newrole When logged in as newrole I can still read the postgres database. How do I revoke any access to the postgres admin database? I have searched a long time but

Does JSONB make PostgreSQL arrays useless?

扶醉桌前 提交于 2019-12-03 10:58:37
Suppose that you want to store "tags" on your object (say, a post). With release 9.4 you have 3 main choices: tags as text[] tags as jsonb tags as text (and you store a JSON string as text) In many cases, 3rd would be out of question since it wouldn't allow query conditional to 'tags' value. In my current development, I don't need such queries, tags are only there to be shown on posts list, not to filter posts. So, choice is mostly between text[] and jsonb . Both can be queried. What would you use? And why? Erwin Brandstetter In most cases I would use a normalized schema with a table option