PostgreSQL compare two jsonb objects

UPDATED CREATE OR REPLACE FUNCTION jsonb_diff_val(val1 JSONB,val2 JSONB) RETURNS JSONB AS $$ DECLARE result JSONB; v RECORD; BEGIN result = val1; FOR v IN SELECT * FROM jsonb_each(val2) LOOP IF result @> jsonb_build_object(v.key,v.value) THEN result = result – v.key; ELSIF result ? v.key THEN CONTINUE; ELSE result = result || jsonb_build_object(v.key,’null’); END IF; END LOOP; … Read more

How to find out if an upsert was an update with PostgreSQL 9.5+ UPSERT?

I believe xmax::text::int > 0 would be the easiest trick: so=# DROP TABLE IF EXISTS tab; NOTICE: table “tab” does not exist, skipping DROP TABLE so=# CREATE TABLE tab(id INT PRIMARY KEY, col text); CREATE TABLE so=# INSERT INTO tab(id, col) VALUES (1,’a’), (2, ‘b’); INSERT 0 2 so=# INSERT INTO tab(id, col) VALUES (3, … Read more

Why isn’t row level security enabled for Postgres views?

Basically because it wasn’t possible to retroactively change how views work. I’d like to be able to support SECURITY INVOKER (or equivalent) for views but as far as I know no such feature presently exists. You can filter access to the view its self with row security normally. The tables accessed by the view will … Read more

How to correctly do upsert in postgres 9.5

The ON CONFLICT construct requires a UNIQUE constraint to work. From the documentation on INSERT .. ON CONFLICT clause: The optional ON CONFLICT clause specifies an alternative action to raising a unique violation or exclusion constraint violation error. For each individual row proposed for insertion, either the insertion proceeds, or, if an arbiter constraint or … Read more

Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

To add the value use the JSON array append opperator (||) UPDATE jsontesting SET jsondata = jsondata || ‘[“newString”]’::jsonb WHERE id = 7; Removing the value looks like this UPDATE jsontesting SET jsondata = jsondata – ‘newString’ WHERE id = 7; Concatenating to a nested field looks like this UPDATE jsontesting SET jsondata = jsonb_set( … Read more

Postgres shuts down immediately when started with docker-compose

If you look at your log output, the following lines appear towards the end: local-postgres9.5 | server stopped local-postgres9.5 | local-postgres9.5 | PostgreSQL init process complete; ready for start up. Apparently, stopping and restarting the Postgres server is part of the initialisation process. In fact, the second-to-last line says local-postgres9.5 | LOG: database system is … Read more

PostgreSQL rename attribute in jsonb field

In UPDATE use delete (-) and concatenate (||) operators, e.g.: create table example(id int primary key, js jsonb); insert into example values (1, ‘{“nme”: “test”}’), (2, ‘{“nme”: “second test”}’); update example set js = js – ‘nme’ || jsonb_build_object(‘name’, js->’nme’) where js ? ‘nme’ returning *; id | js —-+————————- 1 | {“name”: “test”} 2 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)