postgresql-9.1

How to perform a select query in a DO block?

冷暖自知 提交于 2019-11-26 21:13:21
问题 I want to port the below SQL code from MS SQL-Server to PostgreSQL. DECLARE @iStartYear integer DECLARE @iStartMonth integer DECLARE @iEndYear integer DECLARE @iEndMonth integer SET @iStartYear = 2012 SET @iStartMonth = 4 SET @iEndYear = 2016 SET @iEndMonth = 1 ;WITH CTE AS ( SELECT --@iStartYear AS TheStartYear @iStartMonth AS TheRunningMonth ,@iStartYear AS TheYear ,@iStartMonth AS TheMonth UNION ALL SELECT --CTE.TheStartYear AS TheStartYear --@iStartYear AS TheStartYear CTE.TheRunningMonth

Postgres analogue to CROSS APPLY in SQL Server

空扰寡人 提交于 2019-11-26 21:01:43
I need to migrate SQL queries written for MS SQL Server 2005 to Postgres 9.1. What is the best way to substitute for CROSS APPLY in this query? SELECT * FROM V_CitizenVersions CROSS APPLY dbo.GetCitizenRecModified(Citizen, LastName, FirstName, MiddleName, BirthYear, BirthMonth, BirthDay, ..... ) -- lots of params GetCitizenRecModified() function is a table valued function. I can't place code of this function because it's really enormous, it makes some difficult computations and I can't abandon it. Erwin Brandstetter In Postgres 9.3 or later use a LATERAL join: SELECT v.col_a, v.col_b, f.* --

PL/pgSQL checking if a row exists

折月煮酒 提交于 2019-11-26 20:33:54
I'm writing a function in PL/pgSQL, and I'm looking for the simplest way to check if a row exists. Right now I'm SELECTing an integer into a boolean , which doesn't really work. I'm not experienced with PL/pgSQL enough yet to know the best way of doing this. Here's part of my function: DECLARE person_exists boolean; BEGIN person_exists := FALSE; SELECT "person_id" INTO person_exists FROM "people" p WHERE p.person_id = my_person_id LIMIT 1; IF person_exists THEN -- Do something END IF; END; $$ LANGUAGE plpgsql; Update - I'm doing something like this for now: DECLARE person_exists integer; BEGIN

Cannot make @ManyToOne relationship nullable

拜拜、爱过 提交于 2019-11-26 20:14:28
问题 I have a many-to-one relationship that I want to be nullable: @ManyToOne(optional = true) @JoinColumn(name = "customer_id", nullable = true) private Customer customer; Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can anyone explain this? Is there a way to make it work? Note that I use JBoss 7, JPA 2.0 with Hibernate as persistence provider and a PostgreSQL 9.1 database. EDIT : I found the cause of my problem. Apparently it is due to the way I defined the primary key

Generate series of dates - using date type as input

蓝咒 提交于 2019-11-26 19:12:23
Documentation for generate_series says that argument can be int or bigint for generate_series(start, stop) and generate_series(start, stop, step) cases and timestamp or timestamp with time zone for generate_series(start, stop, step interval) . What is the reason that generate_series works also with date type as input and returns timestamp with timezone ? pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day'); generate_series ------------------------ 2014-01-01 00:00:00+01 2014-01-02 00:00:00+01 (2 rows) Thanks to function type resolution we can also pass date values to

How do I import modules or install extensions in PostgreSQL 9.1+?

老子叫甜甜 提交于 2019-11-26 18:54:59
问题 Firstly, if you're not using 9.1+, please refer to this question. How do I install an extension to PostgreSQL 9.1? 回答1: Postgrseql 9.1 provides for a new command CREATE EXTENSION. You should use it to install modules. Modules provided in 9.1 can be found here.. The include, adminpack , auth_delay , auto_explain , btree_gin , btree_gist , chkpass , citext , cube , dblink , dict_int , dict_xsyn , dummy_seclabel , earthdistance , file_fdw , fuzzystrmatch , hstore , intagg , intarray , isn , lo ,

Error creating a spatial database. ERROR : could not load library “/usr/pgsql-9.1/lib/rtpostgis-2.0.so”

两盒软妹~` 提交于 2019-11-26 18:39:00
问题 I am running PostgreSQL 9.1 with Postgis 2.0 on Fedora 15 . When trying to install the raster support, psql -d database -f rtpostgis.sql -v ON_ERROR_STOP=1 I get the following error psql:rtpostgis.sql:46: ERROR: could not load library "/usr/pgsql-9.1/lib/rtpostgis-2.0.so": libhdf5.so.6: cannot open shared object file: No such file or directory A locate libhdf5.so.6 gives the following paths. /usr/lib64/mpich2/lib/libhdf5.so.6 /usr/lib64/mpich2/lib/libhdf5.so.6.0.4 Reinstalling the GDAL from

How to exit from PostgreSQL command line utility: psql

时光总嘲笑我的痴心妄想 提交于 2019-11-26 17:53:13
问题 What command or short key can I use to exit the PostgreSQL command line utility psql ? 回答1: Type \q and then press ENTER to quit psql . UPDATE: 19-OCT-2018 As of PostgreSQL 11 , the keywords " quit " and " exit " in the PostgreSQL command-line interface have been included to help make it easier to leave the command-line tool. 回答2: My usual key sequence is: quit() quit exit() exit q q() !q ^C help Alt + Tab google.com Quit PSQL \q I think veterans of the psql command line usually shorten that

PostgreSQL - dynamic value as table name [duplicate]

泄露秘密 提交于 2019-11-26 16:41:08
问题 Possible Duplicate: Postgres Dynamic Query Function I wish to use the returned string from the query below as a table name for other query. SELECT 'backup_' || TO_CHAR(CURRENT_DATE,'yyyy-mm-dd') as you can see it returns a string. I wish to use it as an input for another query, e.g. CREATE TABLE (SELECT 'backup_' || TO_CHAR(CURRENT_DATE,'yyyy-mm-dd')) AS * SELECT FROM backup Can it be done? Any clue how? 回答1: You will need to use the PL/PgSQL EXECUTE statement, via a DO block or PL/PgSQL

Constraint defined DEFERRABLE INITIALLY IMMEDIATE is still DEFERRED?

陌路散爱 提交于 2019-11-26 16:40:57
In connection with this answer I stumbled upon a phenomenon I cannot explain. Version: PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 64-bit Consider the following demo. Testbed: CREATE TEMP TABLE t ( id integer ,txt text ,CONSTRAINT t_pkey PRIMARY KEY (id) DEFERRABLE INITIALLY IMMEDIATE ); INSERT INTO t VALUES (1, 'one') ,(2, 'two'); 1) UPDATE statement modifying multiple rows: UPDATE t SET id = t_old.id FROM t t_old WHERE (t.id, t_old.id) IN ((1,2), (2,1)); It seems there is a bug in the current implementation? The above UPDATE works though it