postgresql-9.1

Postgres recursive query to update values of a field while traversing parent_id

你说的曾经没有我的故事 提交于 2019-12-06 01:11:19
This is the table user_id | parent_id | lft --------|-----------|----- 1 | | 0 2 | 1 | 0 3 | 1 | 0 4 | 2 | 0 Here is a query to do a CTE from node 1 and traverse all the children of user_id 1 until a leaf is reached and update the value of the travesed chidren lft field to 1 WITH RECURSIVE d AS ( SELECT user_id FROM btrees WHERE user_id = 1 UNION ALL SELECT c.user_id FROM d JOIN btrees c ON c.parent_id = d.user_id ) UPDATE btrees b set lft = 1 FROM d WHERE d.user_id = b.user_id I am just asking for a query that will go in the opposite direction .. ie. from any node to the root node so I can

How do I change the default client_encoding in Postgres?

风流意气都作罢 提交于 2019-12-05 22:39:40
问题 I'm trying to change the default value for the client_encoding configuration variable for a PostgreSQL database I'm running. I want it to be UTF8 , but currently it's getting set to LATIN1 . The database is already set to use UTF8 encoding: application_database=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ----------------------+----------+----------+-------------+-------------+-------------------------------------- postgres | postgres | LATIN1 | en_US |

multiply(num) aggregate function in postgresql

末鹿安然 提交于 2019-12-05 22:13:14
This could be incredibly simple by the documentation is quite on it. Is there a way to aggregate columns via multiplication operator in postgresql. I know i can do count(column) or sum(column), but is there a multiply(column) or product(column) function that i can use. If not, any ideas how to achieve it. I'm using postgres 9.1 regards, Hassan Sure, just define an aggregate over the base multiplication function. E.g. for bigint: CREATE AGGREGATE mul(bigint) ( SFUNC = int8mul, STYPE=bigint ); Example: regress=> SELECT mul(x) FROM generate_series(1,5) x; mul ----- 120 (1 row) See CREATE

How to get unique values from each column based on a condition?

落花浮王杯 提交于 2019-12-05 21:43:19
I have been trying to find an optimal solution to select unique values from each column . My problem is I don't know column names in advance since different table has different number of columns. So first, I have to find column names and I could use below query to do it: select column_name from information_schema.columns where table_name='m0301010000_ds' and column_name like 'c%' Sample output for column names: c1, c2a, c2b, c2c, c2d, c2e, c2f, c2g, c2h, c2i, c2j, c2k, ... Then I would use returned column names to get unique/distinct value in each column and not just distinct row . I know a

Ordering differences between Postgres instances on different machines (same locale)

烈酒焚心 提交于 2019-12-05 17:31:30
I have two Postgres 9.1 instances: one local, installed via Postgres.app on OS X, and one remote, on Heroku. I've ensured that lc_collate is en_US.UTF-8 on both machines but am still seeing different behavior between the two. On my local instance, SELECT 'i' > 'N' returns t whereas remotely it returns f . Given that I've already checked lc_* on both systems, what explains the difference I'm seeing? From the point of view of Unicode, the case ordering is a customization. Excerpt from http://www.unicode.org/reports/tr10 : Case Ordering. Some dictionaries and authors collate uppercase before

Different results for extract epoch on different PostgreSQL servers

谁说胖子不能爱 提交于 2019-12-05 14:42:25
We convert time stamps to epoch, do some math on them and then convert them back to time stamps. All times in the database are TIMESTAMP WITHOUT TIME ZONE . Since the switch to summer time here in the UK times are off by one hour on one server but not on the other so I did a little test: SHOW SERVER_VERSION; SHOW TIMEZONE; SELECT extract(EPOCH FROM TIMESTAMP '1970-01-01'); On one server I get server_version ---------------- 9.1.15 (1 row) TimeZone ---------- GB (1 row) date_part ----------- 0 (1 row) But on the other server_version ---------------- 9.3.6 (1 row) TimeZone ---------- GB (1 row)

Postgresql server remote connection

隐身守侯 提交于 2019-12-05 14:38:40
I am trying to set up a postgresql 9.1 server on ubuntu, for remote access of data. I have postgres properly installed, the server process is running and I am trying to configure it so that I can access the server remotely over the internet from a few other computers outside my LAN. I have already modified my pg_hba.conf with: host all all 0.0.0.0 trust and the postgresql.conf with: listen_addresses = '*' port = 5432 I have additionally modified my iptables to accept connections on port 5432. When I try to connect using psycopg2 on python: conn=psycopg2.connect('host=XX.XX.XX.XX port=5432

Is there data visualisation tool for postgresql which is capable of displaying inter schema relations as well? [closed]

时光怂恿深爱的人放手 提交于 2019-12-05 11:58:13
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Operating system used is linux. I have tried Navicat and SQL Power Architect . They did display relations between tables in the same schema. I have some foreign key constraints which reference tables in a different schema. Am I missing something with respect to Navicat and PostgreSQL Maestro? Can they not

PostgreSQL: Loop Until a Condition is True

两盒软妹~` 提交于 2019-12-05 09:59:16
I am trying to write a query which "loops" through a database starting at a specified value until a condition is true. For example, suppose I have the following entries in TABLE example: id, parent, cond 1, , True 2, 1 , False 3, 1 , False 4, 2 , False ... ... ... I want a query which takes as input (for instance) 4, and will return the values of 2 and 1. The process being that the query matches the id, and if cond==False, will look at the parent (id = 2). Since cond = False in the second row, the "parent" id will be selected (1). Looking at the first row now, since cond=True, the LOOP ends

Unable to syncdb in GeoDjango App

浪尽此生 提交于 2019-12-05 09:37:25
I am having a real trouble in setting up spatial database and syncing it with GeoDjango. I was able to setup the spatial database as per the geodjango documentation and create a django app but when i run python manage.py sqlall world I am getting this, Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/smaranh/django-env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line utility.execute() File "/home/smaranh/django-env/local/lib/python2.7/site-packages/django/core