postgresql-9.1

Printing to screen in .sql file postgres

怎甘沉沦 提交于 2019-11-28 02:24:41
问题 This sounds like it should be a very easy thing to do, however, I cannot find ANYWHERE how to do it. I have a .sql file I am building for an upgrade to my application that alters tables, inserts/updates, etc. I want to write to the screen after every command finishes. So, for instance if I have something like: insert into X... I want to see something like, Starting to insert into table X Finished inserting into table X Is this possible in Postgres? 回答1: If you're just feeding a big pile of

Check if NULL exists in Postgres array

帅比萌擦擦* 提交于 2019-11-28 00:59:40
Similar to this question , how can I find if a NULL value exists in an array? Here are some attempts. SELECT num, ar, expected, ar @> ARRAY[NULL]::int[] AS test1, NULL = ANY (ar) AS test2, array_to_string(ar, ', ') <> array_to_string(ar, ', ', '(null)') AS test3 FROM ( SELECT 1 AS num, '{1,2,NULL}'::int[] AS ar, true AS expected UNION SELECT 2, '{1,2,3}'::int[], false ) td ORDER BY num; num | ar | expected | test1 | test2 | test3 -----+------------+----------+-------+-------+------- 1 | {1,2,NULL} | t | f | | t 2 | {1,2,3} | f | f | | f (2 rows) Only a trick with array_to_string shows the

How to perform a select query in a DO block?

坚强是说给别人听的谎言 提交于 2019-11-27 23:33:47
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 + 1 AS TheRunningMonth --,CTE.TheStartYear + (CTE.TheRunningMonth / 12) AS TheYear ,@iStartYear + (CTE

Alphanumeric case in-sensitive sorting in postgres

我们两清 提交于 2019-11-27 23:11:46
问题 I am new to postrges and want to sort varchar type columns. want to explain the problem with with below example: table name: testsorting order name 1 b 2 B 3 a 4 a1 5 a11 6 a2 7 a20 8 A 9 a19 case sensitive sorting (which is default in postgres) gives: select name from testsorting order by name; A B a a1 a11 a19 a2 a20 b case in-sensitive sorting gives: select name from testsorting order by UPPER(name); A a a1 a11 a19 a2 a20 B b how can i make alphanumeric case in-sensitive sorting in

PostgreSQL 9.1: How to concatenate rows in array without duplicates, JOIN another table

被刻印的时光 ゝ 提交于 2019-11-27 20:28:15
问题 I am using PostgreSQL 9.1 and need help with concatenating multiple rows in one. I need to do that in 2 tables. When I use two times array_agg() functions I get duplicated values in result. Tables: CREATE TABLE rnp (id int, grp_id int, cabinets varchar(15) ); INSERT INTO rnp VALUES (1,'11','cabs1') ,(2,'11','cabs2') ,(3,'11','cabs3') ,(4,'11','cabs4') ,(5,'22','c1') ,(6,'22','c2'); CREATE TABLE ips (id int, grp_id int, address varchar(15)); INSERT INTO ips VALUES (1,'11','NY') ,(2,'11','CA')

Cannot make @ManyToOne relationship nullable

老子叫甜甜 提交于 2019-11-27 20:10:46
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 in the referenced entity Customer : @Entity @Table public class Customer { @Id @GeneratedValue @Column

PostgreSQL with-delete “relation does not exists”

只愿长相守 提交于 2019-11-27 18:03:41
问题 I am using postgreSQL 9.1 and I want to delete duplicates from my table using this tip: https://stackoverflow.com/a/3822833/2239537 So, my query looks like that: WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY code, card_id, parent_id ORDER BY id DESC) RN FROM card) DELETE FROM cte WHERE RN > 1 But it shows me ERROR: relation "cte" does not exist SQL state: 42P01 Character: 157 However this statement works fine: WITH cte AS (SELECT ROW_NUMBER() OVER (PARTITION BY code, card_id, parent_id

Get a timestamp from concatenating day and time columns

不羁岁月 提交于 2019-11-27 17:38:36
问题 I am having day and time fields in database. I want to get the time-stamp by concatenating the day and time. How to do this in PostgreSQL? I have done this: SELECT EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28'); And it is working fine. But when I tried replacing day and time fields I am getting the following error: SELECT EXTRACT(EPOCH FROM TIMESTAMP Day || ' ' || Time); ERROR: syntax error at or near "Day" LINE 1: ...quest_count > 0 AND (EXTRACT(EPOCH FROM TIMESTAMP Day || ' ' || Time))

SELECT .. INTO to create a table in PL/pgSQL

和自甴很熟 提交于 2019-11-27 17:11:14
问题 I want to use SELECT INTO to make a temporary table in one of my functions. SELECT INTO works in SQL but not PL/pgSQL. This statement creates a table called mytable (If orig_table exists as a relation): SELECT * INTO TEMP TABLE mytable FROM orig_table; But put this function into PostgreSQL, and you get the error: ERROR: "temp" is not a known variable CREATE OR REPLACE FUNCTION whatever() RETURNS void AS $$ BEGIN SELECT * INTO TEMP TABLE mytable FROM orig_table; END; $$ LANGUAGE plpgsql; I can

Rails: rake db:create:all fails to connect to PostgreSQL database

牧云@^-^@ 提交于 2019-11-27 16:47:38
问题 I am trying to create a Rails app that uses PostgreSQL. Here is a description of what I did. PostgreSQL setup: I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresql maintained by Martin Pitt. There was PostgreSQL 8.4 installed before; I am not sure if it is still installed or gone. I added a database user with superuser rights to the database that has the same name as my Ubuntu account. I start the database daemon with sudo service postgresql start . I installed pgadmin3, Version 1.14.0