postgresql-9.1

extract week number from date postgres

那年仲夏 提交于 2019-12-20 11:52:39
问题 I would like to extract the week number as: 2015-52 from a date formatted as: 2015-12-27 How can I perform this in postgres? my weeks are calculated from monday to sunday. 回答1: To get the year and the week in a single character value, use to_char() select to_char(current_date, 'IYYY-IW'); IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year). If you need the year and the week number as numbers, use

PostgreSQL date() with timezone

喜你入骨 提交于 2019-12-20 08:39:56
问题 I'm having an issue selecting dates properly from Postgres - they are being stored in UTC, but not converting with the Date() function properly. Converting the timestamp to a date gives me the wrong date if it's past 4pm PST. 2012-06-21 should be 2012-06-20 in this case. The starts_at column datatype is timestamp without time zone . Here are my queries: Without converting to PST timezone: Select starts_at from schedules where id = 40; starts_at --------------------- 2012-06-21 01:00:00

How does one drop a template database from PostgreSQL?

天涯浪子 提交于 2019-12-20 08:35:13
问题 postgres=# DROP DATABASE template_postgis; ERROR: cannot drop a template database http://www.postgresql.org/docs/9.1/static/manage-ag-templatedbs.html makes it seem like if I set template_postgis.datistemplate = false , I'll be able to drop it, but I don't know how to set that. 回答1: postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis'; UPDATE 1 postgres=# DROP DATABASE template_postgis; DROP DATABASE postgres=# 回答2: You can use the alter database command.

How to set up Postgres database for local Rails project?

北城余情 提交于 2019-12-20 08:20:17
问题 I recently got a new machine and would now like to work on my projects from Github. I'm curious as to how to properly set up the Postgres database on my local machine. I have postgresql , pgadmin3 and libpq-dev installed on Ubuntu (12.04). I pull down the project: git clone https://github.com/thebenedict/cowsnhills.git and run: bundle . When I run: rake db:create && rake db:schema:load I get this error: rake db:create && rake db:schema:load FATAL: password authentication failed for user "cnh"

Count rows affected by DELETE

两盒软妹~` 提交于 2019-12-20 05:58:03
问题 I use this code to verify the DELETE sentence, but I am sure you know a better way: CREATE OR REPLACE FUNCTION my_schema.sp_delete_row_table(table_name character varying , id_column character varying , id_value integer) RETURNS integer AS $BODY$ DECLARE BEFORE_ROWS integer; AFTER_ROWS integer; BEGIN EXECUTE 'SELECT count(*) FROM ' || TABLE_NAME INTO BEFORE_ROWS; EXECUTE 'DELETE FROM ' || TABLE_NAME || ' WHERE ' || ID_COLUMN || ' = ' || (ID_VALUE)::varchar; EXECUTE 'SELECT count(*) FROM ' ||

Attribute number 10 exceeds number of columns 0

蹲街弑〆低调 提交于 2019-12-20 05:16:19
问题 This query returns all the rows(around 850+) from the table successfully: select * from my_db_log where date_trunc('day',creation_date) >= to_date('2014-03-05'::text,'yyyy-mm-dd'); But when I add the count(*) with the same query like below: select count(*) from my_db_log where date_trunc('day',creation_date) >= to_date('2014-03-05'::text,'yyyy-mm-dd'); Then it returns me: ********** Error ********** ERROR: attribute number 10 exceeds number of columns 0 SQL state: XX000 FYI: creation_date is

Specifying data type of columns in a values subquery

烈酒焚心 提交于 2019-12-20 02:36:09
问题 Is there a way to specify the data types of the columns in a values subquery? Something like (values (...)) as tmp_name (colname::type) ? Contrived Example Say I have a table with a uuid column: /* setup */ create table foo (id uuid); insert into foo values ('aaaabbbbccccddddeeeeffff00001111'), ('aaaabbbbccccddddeeeeffff00002222'), ('aaaabbbbccccddddeeeeffff00003333'); If I try to join against it using a values subquery, postgres assumes the column is type text and throws a type error (even

Getting name of the current function inside of the function with plpgsql

荒凉一梦 提交于 2019-12-19 14:55:31
问题 Is there anyway from within a plpgsql function that you can get the name of the function? Or even the OID of the function? I know there are some "special" variables (such as FOUND) within plpgsql, but there doesn't seem to be any way of getting this. (Although, I've read where it seems to be possible if your function is written in C). It's not critical, but it would make something I'm doing a little nicer/less fragile. I'm using PostgreSQL v. 9.1.5 回答1: For triggers use TG_NAME to get the

Cast varchar type to date

流过昼夜 提交于 2019-12-19 10:26:36
问题 I'd like to change a specific column in my PostgreSQL database from character_varying type to type date . Date is in the format yyyy:mm:dd I tried to do: alter table table_name alter column date_time type date using (date_time::text::date); But I received an error message: date/time field value out of range: "2011:06:15" 回答1: When you cast text or varchar to date , the default date format of your installation is expected - depending on the datestyle setting in your postgresql.conf . Generally

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

感情迁移 提交于 2019-12-19 03:59:09
问题 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