postgresql-9.3

PostgreSQL: duplicate key value violates unique constraint on UPDATE command

牧云@^-^@ 提交于 2019-12-07 00:26:18
问题 When doing an UPDATE query, we got the following error message: ERROR: duplicate key value violates unique constraint "tableA_pkey" DETAIL: Key (id)=(47470) already exists. However, our UPDATE query does not affect the primary key. Here is a simplified version: UPDATE tableA AS a SET items = ( SELECT array_to_string( array( SELECT b.value FROM tableB b WHERE b.a_id = b.id GROUP BY b.name ), ',' ) ) WHERE a.end_at BETWEEN now() AND now() - interval '1 day'; We ensured the primary key sequence

Create json with column values as object keys

别来无恙 提交于 2019-12-06 22:25:20
问题 I have a table defined like this: CREATE TABLE data_table AS ( id bigserial, "name" text NOT NULL, "value" text NOT NULL, CONSTRAINT data_table_pk PRIMARY KEY (id) ); INSERT INTO data_table ("name", "value") VALUES ('key_1', 'value_1'), ('key_2', 'value_2'); I would like to get a JSON object from this table content, which will look like this: { "key_1":"value_1", "key_2":"value_2" } Now I'm using the client application to parse the result set into JSON format. Is it possible to accomplish

How to return rows of query result in PostgreSQL's function?

余生长醉 提交于 2019-12-06 09:47:14
I've tried following tutorials for many times but failed. Could someone give me some examples please? Here is my code, it prompts that "ERROR:invalid type name 'SETOF RECORD'" create or replace function find() returns SETOF RECORD as $$ declare A SETOF RECORD; begin A=( select x,y from ....... ) CASE WHEN EXISTS A THEN returns query A ELSE returns query ( select x,y from ...... ) END; end; $$ language plpgsql; Ways to declare set returning function that I remember at the moment: --example 1 create or replace function test() returns SETOF RECORD as $$ begin RETURN QUERY SELECT * FROM generate

PSQL: How can I prevent any output on the command line?

不问归期 提交于 2019-12-06 08:46:57
My problem: I'm trying to run a database generation script at the command line via a batch file as part of a TFS build process to enable nightly testing on a known dataset. The scripts we run are outputting Notices, Warnings and some Errors on the command line. I would like to suppress at least the Notices and Warnings, and if possible the Errors as they don't seem to have an impact on the overall success of the scripts. This output seems to be affecting the success or failure of the process as far as the TFS build process is concerned. It's highlighting every line of output from the scripts

PostgreSQL 9.3: How to insert upper case UUID into table

核能气质少年 提交于 2019-12-06 07:30:35
问题 I have the following table with only 1 column that is id which is of type UUID . Table : uuidtest create table uuidtest ( id uuid ); Insertion : I have generated uuid by using uuid_generate_v4() and also done it upper case and inserting it into the table. Try1: insert into uuidtest values('{4B36AFC8-5205-49C1-AF16-4DC6F96DB982}'); Try2: insert into uuidtest values('4B36AFC8-5205-49C1-AF16-4DC6F96DB982'); Now see the result: select * from uuidtest; Ouptput : id uuid ---------------------------

Postgres SELECT* FROM table WHERE column-varchar==“string-example”?

你。 提交于 2019-12-06 07:25:40
问题 I have the following table: CREATE TABLE lawyer ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL UNIQUE, name_url VARCHAR check(translate(name_url, 'abcdefghijklmnopqrstuvwxyz-', '') = '') NOT NULL UNIQUE ); I want to SELECT * FROM lawyer where name_url = "john-doe" 回答1: Character literals are put into single quotes: SELECT * FROM lawyer where name_url = 'john-doe'; See the manual for details: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS 回答2:

Left Join Lateral and array aggregates

你说的曾经没有我的故事 提交于 2019-12-06 04:59:42
I'm using Postgres 9.3. I have two tables T1 and T2 and a n:m relation T1_T2_rel between them. Now I'd like to create a view that in addition to the columns of T1 provides a column that, for each record in T1, contains an array with the primary key ids of all related records of T2. If there are no related entries in T2, corresponding fields of this column shall contain null-values. An abstracted version of my schema would look like this: CREATE TABLE T1 ( t1_id serial primary key, t1_data int ); CREATE TABLE T2 ( t2_id serial primary key ); CREATE TABLE T1_T2_rel ( t1_id int references T1( t1

Is there a way to address all elements of JSON array when creating a constraint in PostgreSQL?

五迷三道 提交于 2019-12-06 04:34:12
问题 Does PostgreSQL provide any notation/method for putting a constraint on each element of a JSON array? An example: create table orders(data json); insert into orders values (' { "order_id": 45, "products": [ { "product_id": 1, "name": "Book" }, { "product_id": 2, "name": "Painting" } ] } '); I can easily add a constraint on the order_id field: alter table orders add check ((data->>'order_id')::integer >= 1); Now I need to do the same with product_id . I can put constraint on idividual array

sequelize with postgres database not working after migration from mysql

限于喜欢 提交于 2019-12-06 02:35:45
I change MySQL databese into postgreSQL in sequelize. But After migration I have issue with upper and lowercase first letter in Table or Model... Before my MySQL version was working properly but after migration I got error message: 500 SequelizeDatabaseError: relation "Users" does not exist My User model: module.exports = function(sequelize, Sequelize) { var User = sequelize.define("User", { // profile userlevel: Sequelize.STRING, restaurant: Sequelize.STRING, access: Sequelize.STRING, optionsid: Sequelize.STRING, email: Sequelize.STRING, name: Sequelize.STRING, gender: Sequelize.STRING,

How to install sqlite or postgresql on windows 8 for a ruby on rails setup?

天涯浪子 提交于 2019-12-06 01:11:46
I've been trying to install a database as part of my ruby on rails setup. I'm running a 64 bit windows 8, a x64 based machine. My ruby version is 2.1.3p242, rails version is 4.0.0, sqlite3 version is 3.8.6 and postgresql version is 9.3 I first tried to install sqlite3 by following the steps given in this SO answer but I get this error `require': Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in config/database.yml is valid. My database.yml has 'sqlite3' as the value for the adapter key for all three environments. Next I try to installed