postgresql-9.5

How to update deeply nested JSON object based on filter criteria in Postgres?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 14:36:03
问题 I have a table mapping_transform with a JSONB column content_json containing something like { "meta": {...}, "mapping": [ ..., { "src": "up", "dest": "down", ... }, ... ] } I want to add a new JSON entry ( "rule_names": [ "some name" ] ) to the JSON object matching src = up and dest = down , which would result in { "meta": {...}, "mapping": [ ..., { "src": "up", "dest": "down", ..., "rule_names": [ "some name" ] }, ... ] } The following query returns the JSON object that meets the filter

Change PostgreSQL password encryption from MD5 to SHA

雨燕双飞 提交于 2019-12-12 13:08:27
问题 Is there a way to change the PostgreSQL password encryption method from MD5 to SHA? If Yes, can you please tell me how? I am using PostgreSQL 9.5 回答1: Pg 10 With PostgreSQL 10, you can set password_encryption to scram-sha-256 . From the docs When a password is specified in CREATE ROLE or ALTER ROLE without writing either ENCRYPTED or UNENCRYPTED , this parameter determines whether the password is to be encrypted. The default value is md5, which stores the password as an MD5 hash. Setting this

Syntax error in UPSERT test code

两盒软妹~` 提交于 2019-12-12 10:23:11
问题 I am trying to test the new PostgreSQL upsert syntax with the following test code, but get the syntax error: test=> CREATE TABLE test1 ( test(> key1 integer PRIMARY KEY check (key1 > 0), test(> key2 integer check (key2 > 0) test(> ); CREATE TABLE test=> CREATE OR REPLACE FUNCTION upsert(IN in_json_array jsonb) test-> RETURNS void AS test-> $func$ test$> UPDATE test1 t SET test$> t.key1 = (obj->>'key1')::int, test$> t.key2 = (obj->>'key2')::int test$> FROM JSONB_ARRAY_ELEMENTS(in_json_array)

How to print the total number of row count in tables in specific schema?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 07:02:18
问题 i want to calculate the number of row counts in tables present in specific schema.i have calculated the row count of the tables but i want output in format:- Total number of Row Count for table_name is :[value] I have prepared the scripts in which i an getting the count of every table present in the schema. But my concern is i want the output like :- total no. of row count in table_name is [value]. something like this #!/bin/bash databasename="$(cat /home/enterprisedb/.bash_profile |grep

Select columns and in a jsonb column only return the last element where meets condition

旧城冷巷雨未停 提交于 2019-12-11 19:06:40
问题 I have table documents , I want to select columns foo and bar. And also the column comments which is jsonb . But in comments I only need the last element that meets condition "isUser":false . "select foo, bar, comments from documents where comments @> '[{"isUser":false}]' limit 1 " /*just limit by 1, the latest comment where isUser = false*/ This is how the json looks liks inside comments column: [{ "text": "1 sample lorem ipsum", "authorId": "0dcd5a36-2778-4fc4-bbc1-112ed61f1362", "timestamp

JSON index much slower than index on text column with same value

拜拜、爱过 提交于 2019-12-11 17:10:02
问题 I have an index: CREATE INDEX index_c_profiles_on_city_state_name_domain ON c_profiles ((data->>'state'), (data->>'city'), name, domain); and another index: CREATE INDEX index_c_profiles_on_state ON c_profiles (state) with a new column I created called 'state' with the value of data->>'state' copied manually over from the jsonb column for each row I tried this query to use the first index: SELECT mm.name, mm.domain, mm.data ->> 'city' as city, mm.data ->> 'state' as state FROM c_profiles as

Insert (if not present) and return id

一世执手 提交于 2019-12-11 17:07:45
问题 I have a unique column. I want to insert a row if it's not already there, and then return the id of that row. INSERT INTO t(a) VALUES ('a') ON CONFLICT DO NOTHING RETURNING t.id; returns nothing at all. Here's a fiddle. I'm looking for how to get 1 each time, whether 'a' was newly inserted or not. 回答1: with i as ( INSERT INTO t(a) VALUES ('a') ON CONFLICT (a) DO NOTHING RETURNING id ) select id from i union all select id from t where a = 'a' limit 1 来源: https://stackoverflow.com/questions

Updating Postgres 9.5 Jsonb by id

邮差的信 提交于 2019-12-11 13:51:45
问题 I've read the docs and stack, but I'm too dumb to get this without asking my specific question. Say I have an array of objects stored in a jsonb column, e.g. [{"id":1, "value":"a"], {"id":2, "value":"b"}] What is the most efficient way to change index 1's value from "b" to "c" if you don't have the index and must search by id=2? To be more specific, I'm writing a react/redux/node real time app, and I don't want to trust the redux state to give the index to update value. Rather, I want the

How to get particular object from jsonb in PostgreSQL?

纵饮孤独 提交于 2019-12-11 03:07:24
问题 I have a table called 'Test' that holds two fields 'qnId' and 'Answers'. 'qnId' stores a uuid and 'Answers' is a jsonb array that roughly looks like this: [{ "user" : "1", "ans" : "some text" }, { "user" : "3", "ans": "some text"}] how can I retrieve the value of "ans" of "user" with value 3 . How can I retrieve the value using normal SQL queries 回答1: demo:db<>fiddle You can use jsonb_array_elements() to expand the array elements into one row each. Afterwards you are able to filter the right

PostgreSQL CURSOR with “order by” clause

女生的网名这么多〃 提交于 2019-12-11 02:47:28
问题 Let's suppose there is a Query called A and it takes 2sec. SELECT ... FROM ... ORDER BY "users_device"."id" # Query A # It contains join clause. # It takes 2sec However, When I run A with declaring CURSOR , it takes 8sec. DECLARE "cursor" NO SCROLL CURSOR WITH HOLD FOR SELECT ... FROM ... ORDER BY "users_device"."id" # It takes 8sec I have tried to compare Query Plan between them and then I found A with CURSOR seems to try to avoid sorting operation. The below is actual query plan. # A