postgresql-9.1

Convert hex string to bigint in Postgres [duplicate]

烈酒焚心 提交于 2019-12-03 16:16:53
This question already has an answer here: Convert hex in text representation to decimal number 5 answers I'd like to convert a hex string as used by HTML into a bigint to then convert it into separate R, G and B values in Postgres via a function written in PL/pgSQL. I can decode the string into bytea like this: hex bytea := decode(hex, 'hex'); And in a query with fixed values this works like a beauty: select ( array[ (cast(x'ffaa33' as bigint) >> 16) % 256, (cast(x'ffaa33' as bigint) >> 8) % 256, cast(x'ffaa33' as bigint) % 256 ] ) But I can't put the two together, passing - for example

Moving average in postgresql

别来无恙 提交于 2019-12-03 16:06:57
问题 I have the following table in my Postgresql 9.1 database: select * from ro; date | shop_id | amount -----------+----------+-------- 2013-02-07 | 1001 | 3 2013-01-31 | 1001 | 2 2013-01-24 | 1001 | 1 2013-01-17 | 1001 | 5 2013-02-10 | 1001 | 10 2013-02-03 | 1001 | 4 2012-12-27 | 1001 | 6 2012-12-20 | 1001 | 8 2012-12-13 | 1001 | 4 2012-12-06 | 1001 | 3 2012-10-29 | 1001 | 3 I am trying to get a moving average comparing data against last 3 Thursdays without including the current Thursday. Here's

Postgres - Is this the right way to create a partial index on a boolean column?

自作多情 提交于 2019-12-03 14:13:21
I have the following table: CREATE TABLE recipemetadata ( --Lots of columns diet_glutenfree boolean NOT NULL, ); Most every row will be set to FALSE unless someone comes up with some crazy new gluten free diet that sweeps the country. I need to be able to very quickly query for rows where this value is true. I've created the index: CREATE INDEX IDX_RecipeMetadata_GlutenFree ON RecipeMetadata(diet_glutenfree) WHERE diet_glutenfree; It appears to work, however I can't figure out how to tell if indeed it's only indexing rows where the value is true. I want to make sure it's not doing something

Saving timestamps in Postgres based on Java dates

╄→гoц情女王★ 提交于 2019-12-03 12:03:18
I have a Postgres database with a table that contains a timestamp ( timeOfProcessing TIMESTAMP ). I have a Java datetime value ( java.util.Date dateTime ) and want to store its value in that timestamp field (without time zone). When I do it using the query "INSERT INTO mytable(..., timeOfCreation, ...) VALUES(..., to_timestamp(" + Long.toString(dateTime.getTime()) + "),...)" and then read the saved value ( SELECT timeOfCreation FROM mytable ), they are different ( resultSet.getTimestamp(...).getTime() is not equal to dateTime.getTime() ). How do I need to change the insert statement in order

deadlock in postgres on simple update query

最后都变了- 提交于 2019-12-03 11:36:05
问题 I'm working with postgres 9.1 and getting deadlock exception under excessive execution of a simple update method. According to the logs the deadlock occurs due to execution of two identical updates at the same time. update public.vm_action_info set last_on_demand_task_id=$1, version=version+1 How does two identical simple updates can deadlock each other ? The error that I'm getting in the log 2013-08-18 11:00:24 IDT HINT: See server log for query details. 2013-08-18 11:00:24 IDT STATEMENT:

Select sum of an array column in PostgreSQL

北慕城南 提交于 2019-12-03 11:08:37
问题 If I have the following table: Table "users" Column | Type | Modifiers ---------------+------------------+----------- id | integer | not null default nextval('users_id_seq'::regclass) monthly_usage | real[] | Where monthly_usage is an array of 12 numbers, i.e. {1.2, 1.3, 6.2, 0.9,...} How can I select the sum of that column? Something along the lines of: SELECT id, sum(monthly_usage) as total_usage from users; Which obviously doesn't work. 回答1: SELECT id, (SELECT SUM(s) FROM UNNEST(monthly

How to do Pivoting in PostgreSQL

大憨熊 提交于 2019-12-03 10:53:36
I am new to PostgreSQL. Suppose I have a table as under colorname Hexa rgb rgbvalue Violet #8B00FF r 139 Violet #8B00FF g 0 Violet #8B00FF b 255 Indigo #4B0082 r 75 Indigo #4B0082 g 0 Indigo #4B0082 b 130 Blue #0000FF r 0 Blue #0000FF g 0 Blue #0000FF b 255 If I do a Pivot in SQL Server as SELECT colorname,hexa,[r], [g], [b] FROM (SELECT colorname,hexa,rgb,rgbvalue FROM tblPivot) AS TableToBePivoted PIVOT ( sum(rgbvalue) FOR rgb IN ([r], [g], [b]) ) AS PivotedTable; I get the output as colorname hexa r g b Blue #0000FF 0 0 255 Indigo #4B0082 75 0 130 Violet #8B00FF 139 0 255 How to do the same

Integration of postgreSQL on WAMP

有些话、适合烂在心里 提交于 2019-12-03 10:27:38
问题 I have just installed the postgreSQL on windows 7. I am trying to integrate postgreSQL with WAMP server. For this i have done the following changes in httpd.conf and php.ini file 1 LoadModule c:/path to libpq.dll in httpd.conf and then 2 extension=php_mod_pgsql.dll , extension=php_pgsql.dll -- enable(reemove ;) in php.ini If I do the above changes the localhost does not work. If I do the second changes the localhost work but does not load the libpq.dll . I checked the pgsql by php script by

Running PostgreSQL with Supervisord

邮差的信 提交于 2019-12-03 08:56:42
I want to run PostgreSQL 9.1 using Supervisor on Ubuntu 10.04. At the moment, I manually start PostgreSQL using the init script: /etc/init.d/postgresql start According to this post: http://nicksergeant.com/using-postgresql-with-supervisor-on-ubuntu-1010/ , I need to modify the PostgreSQL config to make it run on TCP port instead of Unix socket, in order to make PostgreSQL work with Supervisor. I have two questions regarding this approach: Considering this is more of hack, is there any implication (e.g. security/permissions, performance, etc) of doing this? Why cannot we just run the same init

Nearest places from a certain point

不羁岁月 提交于 2019-12-03 06:02:43
问题 I have the following table create table places(lat_lng point, place_name varchar(50)); insert into places values (POINT(-126.4, 45.32), 'Food Bar'); What should be the query to get all places close to particular lat/long? gis is installed. 回答1: If you actually wanted to use PostGIS: create table places( lat_lng geography(Point,4326), place_name varchar(50) ); -- Two ways to make a geography point insert into places values (ST_MakePoint(-126.4, 45.32), 'Food Bar1'); insert into places values (