postgresql-9.2

NOT NULL constraint over a set of columns

扶醉桌前 提交于 2019-11-26 08:35:35
问题 I have a table in Postgres which currently has a NOT NULL constraint on it\'s email column. This table also has a phone column which is optional. I would like the system to accept some records without email but only if these have phone as NOT NULL . In other words, I need a NOT NULL database constraint such that CREATE or UPDATE queries succeed without any errors if either or both of email or phone fields are present. Extending the above further, is it possible in Postgres, to specify a set

Return rows matching elements of input array in plpgsql function

不羁的心 提交于 2019-11-26 07:49:20
问题 I would like to create a PostgreSQL function that does something like the following: CREATE FUNCTION avg_purchases( IN last_names text[] DEFAULT \'{}\' ) RETURNS TABLE(last_name text[], avg_purchase_size double precision) AS $BODY$ DECLARE qry text; BEGIN qry := \'SELECT last_name, AVG(purchase_size) FROM purchases WHERE last_name = ANY($1) GROUP BY last_name\' RETURN QUERY EXECUTE qry USING last_names; END; $BODY$ But I see two problems here: It is not clear to me that array type is the most

Permission denied for relation

╄→гoц情女王★ 提交于 2019-11-26 06:52:43
问题 I tried to run simple sql command: select * from site_adzone; and I got this error ERROR: permission denied for relation site_adzone What could be the problem here? I tried also to do select for other tables and got same issue. I also tried to do this: GRANT ALL PRIVILEGES ON DATABASE jerry to tom; but I got this response from console WARNING: no privileges were granted for \"jerry\" Do you have some idea what can be wrong? 回答1: GRANT on the database is not what you need. Grant on the tables

PostgreSQL 9.2 row_to_json() with nested joins

女生的网名这么多〃 提交于 2019-11-26 06:15:07
问题 I\'m trying to map the results of a query to JSON using the row_to_json() function that was added in PostgreSQL 9.2. I\'m having trouble figuring out the best way to represent joined rows as nested objects (1:1 relations) Here\'s what I\'ve tried (setup code: tables, sample data, followed by query): -- some test tables to start out with: create table role_duties ( id serial primary key, name varchar ); create table user_roles ( id serial primary key, name varchar, description varchar, duty_id

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

守給你的承諾、 提交于 2019-11-26 03:58:53
问题 I am trying to open a program for the first time on Windows XP Pro that uses PostgreSQL 9. I\'m getting an error message that says : A problem was encountered while trying to log into or create the production database. Details: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified In my ODBC manager, I have a list of User DSN\'s and System DSN\'s. I tried installing a postgres odbc driver to see if that would help, but it didn\'t. There is a connect.dat

How do I query using fields inside the new PostgreSQL JSON datatype?

霸气de小男生 提交于 2019-11-25 23:30:15
问题 I am looking for some docs and/or examples for the new JSON functions in PostgreSQL 9.2. Specifically, given a series of JSON records: [ {name: \"Toby\", occupation: \"Software Engineer\"}, {name: \"Zaphod\", occupation: \"Galactic President\"} ] How would I write the SQL to find a record by name? In vanilla SQL: SELECT * from json_data WHERE \"name\" = \"Toby\" The official dev manual is quite sparse: http://www.postgresql.org/docs/devel/static/datatype-json.html http://www.postgresql.org