postgresql-9.1

PostgreSQL use value from previous row if missing

点点圈 提交于 2019-12-01 07:03:38
I have a following query: WITH t as ( SELECT date_trunc('hour', time_series) as trunc FROM generate_series('2013-02-27 22:00'::timestamp, '2013-02-28 2:00', '1 hour') as time_series GROUP BY trunc ORDER BY trunc ) SELECT DISTINCT ON(trunc) trunc, id FROM t LEFT JOIN ( SELECT id, created, date_trunc('hour', created) as trunc_u FROM event ORDER BY created DESC ) u ON trunc = trunc_u which yields the following result: "2013-02-27 22:00:00"; "2013-02-27 23:00:00";2 "2013-02-28 00:00:00";5 "2013-02-28 01:00:00"; "2013-02-28 02:00:00"; Table event has id , created and some other columns, but only

Ubuntu 12.04, PostgreSQL-9.1 - Can't access $libdir/postgis-2.0

孤者浪人 提交于 2019-12-01 06:18:59
Good morning, Collective Intelligence. I've recently had to resurrect a postgresql-9.1 database by plugging the raw data directory files into a fresh install. All my data is intact, except I can't open up my PostGIS spatial data. I'm able to enable spatial data on new databases with: CREATE extension postgis; However, trying to access data in the recovered spatial databases results in an error. For example, SELECT PostGIS_full_version(); throws an error: ERROR: could not access file "$libdir/postgis-2.0": No such file or directory SQL state: 58P01 Context: SQL statement "SELECT postgis_lib

Copy data between two tables in PostgreSQL using dblink.sql

大憨熊 提交于 2019-12-01 05:48:57
问题 I am using PostgreSQL 9.1. I need to transfer required columns from one table of one database into another table of another database, but not schema. I found that dblink.sql file has to be there in share/contrib . But my contrib folder is empty. Where can I download the dblink.sql file and can execute my query? When I execute the query now it shows an error message: cross database reference is not possible ... Can anyone help me how to transfer the data between two databases? 回答1: After you

Exclusion constraint on a bitstring column with bitwise AND operator

瘦欲@ 提交于 2019-12-01 05:33:49
So I was just reading about Exclusion Constraints in PostgreSQL and I couldn't seem to find a way to use bitwise operators on bitstrings, and I was wondering if it was possible. My use case is I have a name: text column and a value: bit(8) column. And I wanted to create a constraint that basically says this: ADD CONSTRAINT route_method_overlap EXCLUDE USING gist(name WITH =, value WITH &) But this doesn't work since operator &(bit,bit) is not a member of operator family "gist_bit_ops" I assume this is because the bit_ops & operator doesn't return a boolean. But is there a way to do what I'm

PostgreSQL date difference

末鹿安然 提交于 2019-12-01 04:14:40
I have a PostgreSQL function which calculates date difference: CREATE OR REPLACE FUNCTION testDateDiff () RETURNS int AS $BODY$ DECLARE startDate TIMESTAMP; DECLARE endDate TIMESTAMP; DECLARE diffDatePart int ; BEGIN Select evt_start_date From events Where evt_id = 5 INTO startDate ; Select evt_start_date From events Where evt_id = 6 INTO endDate ; SELECT EXTRACT(day FROM TIMESTAMP startDate - endDate) INTO diffDatePart; RETURN diffDatePart; END; $BODY$ LANGUAGE plpgsql COST 100 If dates are subtracted directly then difference is calculated. But in my case dates are present in variables as

Ubuntu 12.04, PostgreSQL-9.1 - Can't access $libdir/postgis-2.0

拥有回忆 提交于 2019-12-01 03:50:02
问题 Good morning, Collective Intelligence. I've recently had to resurrect a postgresql-9.1 database by plugging the raw data directory files into a fresh install. All my data is intact, except I can't open up my PostGIS spatial data. I'm able to enable spatial data on new databases with: CREATE extension postgis; However, trying to access data in the recovered spatial databases results in an error. For example, SELECT PostGIS_full_version(); throws an error: ERROR: could not access file "$libdir

Months between two dates function

喜夏-厌秋 提交于 2019-12-01 03:14:29
In oracle i can find out no:of months between using MONTHS_BETWEEN function. In postgres i am using extract function for this. eg.like select extract(year from age(current_date, '2012-12-09')) * 12 + extract(month from age(current_date, '2012-12-09')) Is there any other ways(built in functions) in postgres?? This is easy to re-implement in PostgreSQL just using SQL functions to tidy up what you've already got: create function months_of(interval) returns int strict immutable language sql as $$ select extract(years from $1)::int * 12 + extract(month from $1)::int $$; create function months

Exclusion constraint on a bitstring column with bitwise AND operator

天涯浪子 提交于 2019-12-01 03:02:46
问题 So I was just reading about Exclusion Constraints in PostgreSQL and I couldn't seem to find a way to use bitwise operators on bitstrings, and I was wondering if it was possible. My use case is I have a name: text column and a value: bit(8) column. And I wanted to create a constraint that basically says this: ADD CONSTRAINT route_method_overlap EXCLUDE USING gist(name WITH =, value WITH &) But this doesn't work since operator &(bit,bit) is not a member of operator family "gist_bit_ops" I

Why does PostgreSQL treat my query differently in a function?

隐身守侯 提交于 2019-12-01 00:11:37
I have a very simple query that is not much more complicated than: select * from table_name where id = 1234 ...it takes less than 50 milliseconds to run. Took that query and put it into a function: CREATE OR REPLACE FUNCTION pie(id_param integer) RETURNS SETOF record AS $BODY$ BEGIN RETURN QUERY SELECT * FROM table_name where id = id_param; END $BODY$ LANGUAGE plpgsql STABLE; This function when executed select * from pie(123); takes 22 seconds. If I hard code an integer in place of id_param, the function executes in under 50 milliseconds. Why does the fact that I am using a parameter in the

Set custom timezone in Django/PostgreSQL (Indian Standard Time)

陌路散爱 提交于 2019-11-30 23:10:29
In Django documentation for setting timezone , the list of available choices for timezone are actually postgres timezone parameter strings. So it seems Django uses Postgres for retrieving time. If so, then the problem is that IST is used to denote both Indian & Israel Standard Time, but postgres uses IST for Israel Standard Time (potentially confusing over a 6th of world's population) and there is NO timezone string for Indian Standard Time. Not just that, Postgres also misses timezone for some other countries like Nepal (GMT+5:30) and some Pacific Islands. So, is there any way by which I can