postgresql-9.2

PostgreSQL - properly change ID of table row

爱⌒轻易说出口 提交于 2019-12-06 21:49:32
问题 How to change id of some table's row? Like: UPDATE table SET id=10 WHERE id=5; But in way that it would cascade changes to every other table that references this table with that id? I want to do this, because I need to import data from another database which has most of the same tables, but ids are different. So if ids would match old database, it would be easier to import data correctly. 回答1: Suppose you have these two tables: create table referenced (id integer primary key); create table

Problems with a PostgreSQL upsert query

假如想象 提交于 2019-12-06 06:36:45
问题 I'm trying to update the database by either updating or inserting a new record into the vote_user_table . The table is defined as follows: Column | Type | Modifiers -----------+---------+-------------------------------------------------------------- id | integer | not null default nextval('vote_user_table_id_seq'::regclass) review_id | integer | not null user_id | integer | not null positive | boolean | default false negative | boolean | default false Indexes: "vote_user_table_pkey" PRIMARY

Postgres Query JSON Array that contains something

依然范特西╮ 提交于 2019-12-06 03:30:41
问题 Postgres has this JSON datatype and I am wondering how can I query data inside a JSON array ? I am using Postgres 9.3.1 I have inserted into PUBLISHER table that has these 2 fields name: string and data: json INSERT INTO PUBLISHER (name, data) VALUES ('PUB1', [{"code":"PA1","author":"James","type":"Novel"},{"code":"PA2","author":"John","type":"Science"}] INSERT INTO PUBLISHER (name, data) VALUES ('PUB2', [{"code":"PA1","author":"Dickens","type":"Novel"},{"code":"PA2","author":"Tom","type":

cannot copy CSV into postgreSQL table : timestamp column won't accept the empty string

戏子无情 提交于 2019-12-05 23:19:38
问题 I want to import a CSV file into version 9.2 but the CSV file has double-quote double-quote in the final column position to represent a NULL value: "2","1001","9","2","0","0","130","","2012-10-22 09:33:07.073000000","" which is mapped to a column of type Timestamp. postgreSQL doesn't like the "". I've tried to set the NULL option but maybe I'm not doing it correctly? I've tried NULL as '"" and NULL '' and NULL as '' and NULL "" but without success; here's my command: COPY SCH.DEPTS FROM 'H:

Handling EXCEPTION and return result from function

拈花ヽ惹草 提交于 2019-12-05 22:58:13
This is my code CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER AS $$ DECLARE res INTEGER; BEGIN res := 100 / arg; BEGIN EXCEPTION WHEN division_by_zero THEN RETURN 999; END; RETURN res; END; $$ LANGUAGE plpgsql; That is, I need returned "999", if happened division by zero, but this: SELECT test_excep(0) returns error: division by zero CONTEXT: PL/pgSQL function test_excep(integer) line 4 at assignment What is wrong in my code? The EXCEPTION clause needs to be in the same block as the exception. For instance: CREATE OR REPLACE FUNCTION test_excep (arg integer) RETURNS

Check for value with current_setting()

与世无争的帅哥 提交于 2019-12-05 12:57:21
问题 I'm trying to work with current_setting() . I came up with this: CREATE OR REPLACE FUNCTION process_audit() RETURNS TRIGGER AS $audit$ DECLARE user_id integer; BEGIN BEGIN user_id := current_setting('hws.current_user_id'); EXCEPTION WHEN OTHERS THEN user_id := NULL; END; ... RETURN NULL; END; $audit$ LANGUAGE plpgsql; The setting is set via: SELECT set_config('hws.current_user_id', '5', true); -- true = local setting -> only visible in current transaction The problem is, that current_setting(

Dropping column in Postgres on a large dataset

天大地大妈咪最大 提交于 2019-12-05 12:08:20
问题 So I have a table with a large dataset and this table has a three columns that I would like to drop. The question is: how will Postgres deal with it? Will it walk through every entry or will it just update mapping info without much overhead? Can I just make an ALTER TABLE or should I use swap-table in this particular case? And, if it makes any difference, all three columns have fixed length (two integers and one numeric). I'm sorry if it's been asked already, but Google couldn't find any

How can I achieve the same Postgres collation behavior in Linux as that in Mac OS?

喜你入骨 提交于 2019-12-05 05:36:14
When switching between Mac OS (development) to Linux (production server), the collation for Postgres changes since collation in Postgres is dependent on the underlying operating system config. How can I achieve the same Postgres collation behavior in Linux as that which I have in Mac OS? I am running Postgres 9.2 The locales on OS X are sufficiently broken and the ones on Linux (glibc) are sufficiently not, so that you are going to have difficulties sorting this out successfully. The only way would be to use the lowest common denominator, which is the C locale. But if you are developing an

PostgreSQL 9.2.4 (Windows 7) - Service won't start, “could not load pg_hba.conf”

爷,独闯天下 提交于 2019-12-05 03:51:34
I am trying to get Postgres 9.2.4 to run as a service on Windows 7. After installing postgres, the service was running fine. However, after setting postgres up as a server for another program, the service stopped running. When I try to start the service now, I get a message saying : "The postgresql-x64-9.2 - PostgreSQL Server 9.2 service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs." When I try running the program that should use the database server, I get this error : "A problem was encountered while

How to count number of digits in Postgres integer data type?

微笑、不失礼 提交于 2019-12-05 03:45:02
问题 I need to restrict the number of digits in the 'integer' type fields to 6 or 7 in postgres. Basically it should accept 123456 or 1234567 but it should not accept 12345 or 12345678. How can I achieve this?? 回答1: Have a check constraint with check (value>99999 and value<=9999999) 回答2: You can use floor(log(i)+1 to get the number of digits in a number (that's base10 log). DB> CREATE TEMP TABLE t ( i integer CONSTRAINT i_has_6_or_7_digits CHECK (floor(log(abs(i))+1) BETWEEN 6 AND 7) ); CREATE