postgresql-9.5

PostgreSQL - create an auto-increment column for non-primary key

你。 提交于 2019-12-11 01:36:57
问题 I am with PostgreSQL 9.5 X64 integrated with the open-source Parse Server. My table has the following structure. objectId (text with fixed 10 characters), item_id (integer), item_name (text with various length) The objectId is the primary key due to use of Parse Server. It is automatically generated by Parse Server. The item_id is not a primary key. I would like to have item_id automatically increment by 1 when a new record is created. How can this be achieved in Create Table? 回答1: You may

Pg-promise performance boost : ON CONFLICT

☆樱花仙子☆ 提交于 2019-12-10 19:57:42
问题 I'm trying to follow the performance pattern recommended by the pg-promise library author here. Basically Vitaly recommends to do so with inserts : var users = [['John', 23], ['Mike', 30], ['David', 18]]; // We can use Inserts as an inline function also: db.none('INSERT INTO Users(name, age) VALUES $1', Inserts('$1, $2', users)) .then(data=> { // OK, all records have been inserted }) .catch(error=> { // Error, no records inserted }); Using the following helper function : function Inserts

locale issue while upgrading data directory from PostgreSQL 8.4 to 9.5

一个人想着一个人 提交于 2019-12-10 18:07:33
问题 I am upgrading PostgreSQL 8.4 to 9.5 on Windows. Following are the steps that are to be followed: Install PostgreSQL 9.5 (8.4 is already installed). Migrate data from 8.4's data dir to 9.5's data dir using pg_upgrade utility. After successful upgrade, uninstall PostgreSQL 8.4 Here the issue is that PostgreSQL 8.4 could be installed with any locale (English or French etc.) and to have successful data directory upgrade, PostgreSQL 9.5 must be installed with the same locale as that of 8.4.

psql: server does not support SSL, but SSL was required

别来无恙 提交于 2019-12-10 13:22:44
问题 Trying to connect to postgresql server using command prompt. Command used: psql "sslmode=require host=localhost dbname=test" Error thrown: psql: server does not support SSL, but SSL was required Please help me out on this. Thanks. 回答1: This link suggests that you might try psql "sslmode=disable host=localhost dbname=test" or (probably better) psql "sslmode=allow host=localhost dbname=test" That way you should be able to connect to your server. 来源: https://stackoverflow.com/questions/36350600

Postgres Tutorial: pg_restore: [archiver] input file does not appear to be a valid archive

一笑奈何 提交于 2019-12-10 03:11:15
问题 I'm working through the Postgres DVD tutorial and am running into issues importing their sample database. Running pg_restore -U postgres -d dvdrental ~[filepath]/dvd-database.tar.gz gives me pg_restore: [archiver] input file does not appear to be a valid archive . My process so far has been the following: Download the dvdrental.zip file Extract it to a .tar using tar czf dvd-database.tar.gz dvdrental.zip (I've also tried extracting the zip to a folder first with the same result, as well as

Can two concurrent but identical DELETE statements cause a deadlock?

时间秒杀一切 提交于 2019-12-09 10:20:56
问题 Assume some_table has two rows, with primary key 1 and 2 . The following sequence of statements can cause a deadlock: session 1: begin; session 2: begin; session 1: DELETE FROM my_table WHERE my_key = 1; session 2: DELETE FROM my_table WHERE my_key = 2; session 1: DELETE FROM my_table WHERE my_key = 2; session 2: DELETE FROM my_table WHERE my_key = 1; The deadlock would not have occurred if both sessions deleted in the same order. Now, coming to my question, what happens if the DELETE

Postgresql IN operator Performance: List vs Subquery

爷,独闯天下 提交于 2019-12-08 21:38:31
问题 For a list of ~700 ids the query performance is over 20x slower than passing a subquery that returns those 700 ids. It should be the opposite. e.g. (first query takes under 400ms, the later 9600 ms) select date_trunc('month', day) as month, sum(total) from table_x where y_id in (select id from table_y where prop = 'xyz') and day between '2015-11-05' and '2016-11-04' group by month is 20x faster on my machine than passing the array directly: select date_trunc('month', day) as month, sum(total)

Effectively searching through entire 1 level nested JSONB in Postgres

左心房为你撑大大i 提交于 2019-12-08 19:14:12
问题 Let's say we need to check if a jsonb column contains a particular value matching by a substring in any of the value (non-nested, only first level). How does one effectively optimize a query to search entire JSONB column (this means every key) for a value? Is there some good alternative to doing ILIKE %val% on jsonb datatype casted to text? jsonb_each_text(jsonb_column) ILIKE '%val%' As an example consider this data: SELECT '{ "col1": "somevalue", "col2": 5.5, "col3": 2016-01-01, "col4":

Use INSERT … ON CONFLICT DO NOTHING RETURNING failed rows

时光总嘲笑我的痴心妄想 提交于 2019-12-08 06:51:01
问题 Suppose I have the following table: CREATE TABLE tags ( id int PK, name varchar(255), CONSTRAINT name_unique UNIQUE(name) ) I need a query that will insert tags that do not exists and return ids for all requested tags. Consider the following: INSERT INTO tags (name) values ('tag10'), ('tag6'), ('tag11') ON CONFLICT DO NOTHING returning id, name The output of this query is: +---------------+ | id | name | |---------------| | 208 | tag10 | |---------------| | 209 | tag11 | +---------------+

How can I review all database and object grants for a role?

笑着哭i 提交于 2019-12-07 18:51:53
问题 I am trying to audit all of the permissions for an application before release and I want to ensure no role has more access than it needs. I have looked at the different functions and system tables, but everything is very piecemeal. Is there a good query or method to be able to dump out every grant a particular role has? I am using pg 9.5. 回答1: The column relacl of the system catalog pg_class contains all informations on privileges. Example data in schema public owned by postgres with grants