postgresql-9.1

How to add column if not exists on PostgreSQL?

别来无恙 提交于 2019-11-26 15:24:47
问题 Question is simple. How to add column x to table y , but only when x column doesn't exist ? I found only solution here how to check if column exists. SELECT column_name FROM information_schema.columns WHERE table_name='x' and column_name='y'; 回答1: Here's a short-and-sweet version using the "DO" statement: DO $$ BEGIN BEGIN ALTER TABLE <table_name> ADD COLUMN <column_name> <column_type>; EXCEPTION WHEN duplicate_column THEN RAISE NOTICE 'column <column_name> already exists in <table_name>.';

How to create a new database with the hstore extension already installed?

百般思念 提交于 2019-11-26 15:16:34
问题 Recently I went into trouble trying to use hstore with Django. I installed hstore this way: $ sudo -u postgres psql postgres=# CREATE EXTENSION hstore; WARNING: => is deprecated as an operator name DETAIL: This name may be disallowed altogether in future versions of PostgreSQL. CREATE EXTENSION postgres=# \dx List of installed extensions Name | Version | Schema | Description ---------+---------+------------+-------------------------------------------------- hstore | 1.0 | public | data type

I forgot the password I entered during postgres installation

旧时模样 提交于 2019-11-26 12:40:50
I either forgot or mistyped (during the installation) the password to the default user of Postgres. I can't seem to be able to run it and I get the following error: psql: FATAL: password authentication failed for user "hisham" hisham-agil: hisham$ psql Is there anyway to reset the password or how do I create a new user with superuser privileges? I am new to Postgres and just installed it for the first time. I am trying to use it with Rails and I am running Mac OS X Lion. Arsen7 find the file pg_hba.conf - it may be located, for example in /etc/postgresql-9.1/pg_hba.conf . cd /etc/postgresql-9

Generate series of dates - using date type as input

断了今生、忘了曾经 提交于 2019-11-26 12:16:33
问题 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

Rails 3 - can&#39;t install pg gem

橙三吉。 提交于 2019-11-26 12:03:31
When I try to run bundle (bundle install), I all the time get Installing pg (0.13.2) with native extensions Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /Users/ryan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file

Postgres analogue to CROSS APPLY in SQL Server

China☆狼群 提交于 2019-11-26 09:03:37
问题 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

PL/pgSQL checking if a row exists

久未见 提交于 2019-11-26 09:02:21
问题 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

Dynamically generate columns for crosstab in PostgreSQL

做~自己de王妃 提交于 2019-11-26 09:01:12
问题 I am trying to create crosstab queries in PostgreSQL such that it automatically generates the crosstab columns instead of hardcoding it. I have written a function that dynamically generates the column list that I need for my crosstab query. The idea is to substitute the result of this function in the crosstab query using dynamic sql. I know how to do this easily in SQL Server, but my limited knowledge of PostgreSQL is hindering my progress here. I was thinking of storing the result of

Generating time series between two dates in PostgreSQL

China☆狼群 提交于 2019-11-26 07:59:01
I have a query like this that nicely generates a series of dates between 2 given dates: select date '2004-03-07' + j - i as AllDate from generate_series(0, extract(doy from date '2004-03-07')::int - 1) as i, generate_series(0, extract(doy from date '2004-08-16')::int - 1) as j It generates 162 dates between 2004-03-07 and 2004-08-16 and this what I want. The problem with this code is that it wouldn't give the right answer when the two dates are from different years, for example when I try 2007-02-01 and 2008-04-01 . Is there a better solution? Can be done without conversion to/from int (but to

Compute percents from SUM() in the same SELECT sql query

痴心易碎 提交于 2019-11-26 06:08:17
问题 In the table my_obj there are two integer fields: (value_a integer, value_b integer); I try to compute how many time value_a = value_b , and I want to express this ratio in percents. This is the code I have tried: select sum(case when o.value_a = o.value_b then 1 else 0 end) as nb_ok, sum(case when o.value_a != o.value_b then 1 else 0 end) as nb_not_ok, compute_percent(nb_ok,nb_not_ok) from my_obj as o group by o.property_name; compute_percent is a stored_procedure that simply does (a * 100)