postgresql

How can I update multiple columns on multiple rows in postgresql using psycopg2

别来无恙 提交于 2021-01-28 07:30:32
问题 I have a somewhat complex sql query that should update multiple columns on multiple rows in a table. Am trying to pass the multiple parameters to the query and also loop though the data to be updated through psycopg2 but I can't figure out a way to do this. Here is the sample data I want to loop through. data = [(214, 'Feb', 545), (215, 'March', 466)] So far here is the sql query I have query = """ UPDATE table_1 SET date_from = (CASE version WHEN 1 THEN '1900-01-01' ELSE ( SELECT date_to

PostgreSQL使用clickhousedb_fdw访问ClickHouse

风流意气都作罢 提交于 2021-01-28 07:22:46
作者:杨杰 简介 PostgreSQL FDW是一种外部访问接口,它可以被用来访问存储在外部的数据,这些数据可以是外部的PG数据库,也可以mysql、ClickHouse等数据库。 ClickHouse是一款快速的开源OLAP数据库管理系统,它是面向列的,允许使用SQL查询实时生成分析报告。 clickhouse_fdw是一个开源的外部数据包装器(FDW)用于访问ClickHouse列存数据库。 目前有以下两款clickhouse_fdw: https://github.com/adjust/clickhouse_fdw 一直持续不断的有提交,目前支持PostgreSQL 11-13 https://github.com/Percona-Lab/clickhousedb_fdw 之前有一年时间没有动静,最近一段时间刚从adjust/clickhouse_fdw merge了一下,目前也支持PostgreSQL 11-13。 本文就以adjust/clickhouse_fdw为例。 安装 # libcurl >= 7.43.0 yum install libcurl-devel libuuid-devel git clone https://github.com/adjust/clickhouse_fdw.git cd clickhouse_fdw mkdir build && cd

Why does the postgres tx id not start at 1 for a newly created database?

余生长醉 提交于 2021-01-28 07:15:21
问题 I am trying to understand the details of how postgres deals with transactions; I noticed that when the execute the following commands on psql I get a txid that is at 518526 instead of a low number like 0 or 1 which is what I expected would happen. postgres=# CREATE DATABASE test; CREATE DATABASE postgres=# \connect test; You are now connected to database "test" as user "postgres". test=# begin; BEGIN test=# select txid_current(); txid_current -------------- 518526 (1 row) On a second psql

Postgres frequency count across arrays

女生的网名这么多〃 提交于 2021-01-28 06:46:52
问题 I have a column of text[]. How do I get a frequency count of all the objects across the column? Example: col_a -------- {a, b} {a} {b} {a} Output should be: col_a | count ---------------- a | 3 b | 2 My query: with all_tags as ( select array_agg(c) from ( select unnest(tags) from message_tags ) as dt(c) ) select count(*) from all_tags; 回答1: figured it out: -- Collapse all tags into one array with all_tags as ( select array_agg(c) as arr from ( select unnest(ner_tags) from message_tags ) as dt

PostgreSQl function return multiple dynamic result sets

我与影子孤独终老i 提交于 2021-01-28 06:26:27
问题 I have an old MSSQL procedure that needs to be ported to a PostgreSQL function. Basically the SQL procedure consist in a CURSOR over a select statement. For each cursor entity i have three select statements based on the current cursor output. FETCH NEXT FROM @cursor INTO @entityId WHILE @@FETCH_STATUS = 0 BEGIN SELECT * FROM table1 WHERE col1 = @entityId SELECT * FROM table2 WHERE col2 = @entityId SELECT * FROM table3 WHERE col3 = @entityId END The tables from the SELECT statements have

PostgreSQL dates function output to a french language

我是研究僧i 提交于 2021-01-28 06:23:01
问题 How can I output the result of PostgreSQL dates function as to_char to french language, for example the output of: select to_char(current_date, 'Day') ; should be (a french name for a day): Mardi instead of english of day (e.g. Monady) 回答1: You would need to set the display of date/time (LC_TIME) to french, and to query not the Day but rather the localizable day TMDay using the TM prefix. show LC_TIME; SET LC_TIME = 'French'; select to_char(current_date, 'TMDay') ; to_char --------- Mardi (1

Eliminate double quotes from a json field in a selective manner in PostgreSQL

杀马特。学长 韩版系。学妹 提交于 2021-01-28 06:13:29
问题 I'm a new user of a PostgreSQL database with a json field which causes me some trouble. In fact, string values have been inserted into this json filed with double quote when they are supposed to be numerical values. I have a field named "test_json" which is composed of for example these 2 rows: {"test_name": "Full", "parameter1": "4.219", "parameter2": 4.4137} {"test_name": "Full", "parameter1": "3.758", "parameter2": 4.159} I would like the 2 lines above to be after correction in this form:

php postgres from query to fetching rows in theory

孤人 提交于 2021-01-28 06:04:24
问题 I would like to know what precisely is happening since php script runs the query to the moment when database returns data and php script starts to fetch them. In theory, I have a Postgre database with huge table, talking about 10/20 million records with 20+ columns and I have a php script which is requesting all that rows from database to be placed in, lets say, some file. My knowledge is: php script runs the pg_query command with sql query through postgres php driver/extension query is

php postgres from query to fetching rows in theory

谁说我不能喝 提交于 2021-01-28 06:02:22
问题 I would like to know what precisely is happening since php script runs the query to the moment when database returns data and php script starts to fetch them. In theory, I have a Postgre database with huge table, talking about 10/20 million records with 20+ columns and I have a php script which is requesting all that rows from database to be placed in, lets say, some file. My knowledge is: php script runs the pg_query command with sql query through postgres php driver/extension query is

Creating Table or Insert into with parallel workers

自闭症网瘾萝莉.ら 提交于 2021-01-28 05:55:35
问题 I'm running some quite large and expensive queries. After reading a bit on parallel queries, I upgraded my postgres database to 10.4 (fromer was 9.5) and am now able to make use of parallel workers. The queries I normally run make use of either CREATE TABLE AS or INSERT INTO , because I want to run further queries on the computed results. Now it seems that neither of those components is supporting parallel workers. I got the hint that I can use \o to output the querie result into a file, then