psycopg2

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

SQLAlchemy: How to represent data in Python differently than in the database

让人想犯罪 __ 提交于 2021-01-28 06:02:22
问题 In a (postgres) SQLAlchemy model/class, I have several columns that are 'price' columns. I have read that using numeric/money/float types for this sort of data is not a good idea, so I'm storing as INTs (pennies). I have created validators for these columns that will multiply the input value by 100 and cast to an INT upon insert and update, so that is taken care of. How do I do the reverse? When I select this data, I want to cast these values to a float and then divide by 100. I cannot seem

SQLAlchemy: How to represent data in Python differently than in the database

假装没事ソ 提交于 2021-01-28 05:53:16
问题 In a (postgres) SQLAlchemy model/class, I have several columns that are 'price' columns. I have read that using numeric/money/float types for this sort of data is not a good idea, so I'm storing as INTs (pennies). I have created validators for these columns that will multiply the input value by 100 and cast to an INT upon insert and update, so that is taken care of. How do I do the reverse? When I select this data, I want to cast these values to a float and then divide by 100. I cannot seem

PostgreSQL Connection Timed Out OperationalError on Django and new server

一曲冷凌霜 提交于 2021-01-27 19:01:03
问题 After my server got compromised, I've had to migrate to a new droplet - both on DigitalOcean, Debian Jessie, so I shouldn't have the Heroku issues that a lot of the similar questions seem to cover. I copied the database over using pg_dump, and it seems intact. PostgreSQL is installed, and running, but whenever I try to utilise the database from the Django ORM, I get the error - OperationalError: could not connect to server: Connection timed out Is the server running on host "localhost" (127.0

psycopg2 register_composite from sqlalchemy

纵然是瞬间 提交于 2021-01-27 12:56:58
问题 is it possible to somehow use function register_composite from psycopg2, when i am using sqlalchemy to connect to postgresql database? My problem is that I want SQLAlchemy to handle custom composite type that i created in postgresql like this: CREATE TYPE card AS (value int, suit text); Sqlalchemy returns me values of this type as an string and I would like to somhow learn sqlalchemy my new type. If found some information about creating custom composite types in SQL alchemy ORM, but I am

psycopg2.extras.DictCursor does not give me column names

一曲冷凌霜 提交于 2021-01-27 12:12:01
问题 I am using psycopg2 to access the data from the Postgres database. I am using psycopg2.extras.DictCursor for getting the data in a dict-like form using the following query: try: self.con = psycopg2.connect(dbname=self.db, user=self.username, host=self.host, port=self.port) cur = self.con.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute("SELECT * FROM card") result = cur.fetchall() print result I get the output as a list: [['C01', 'card#1', 'description#1', '1'], ['C02', 'card#2',

Deal with Postgresql Error -canceling statement due to conflict with recovery- in psycopg2

拈花ヽ惹草 提交于 2021-01-27 05:26:26
问题 I'm creating a reporting engine that makes a couple of long queries over a standby server and process the result with pandas. Everything works fine but sometimes I have some issues with the execution of those queries using a psycopg2 cursor: the query is cancelled with the following message: ERROR: cancelling statement due to conflict with recovery Detail: User query might have needed to see row versions that must be removed I was investigating this issue PostgreSQL ERROR: canceling statement

Deal with Postgresql Error -canceling statement due to conflict with recovery- in psycopg2

烂漫一生 提交于 2021-01-27 05:25:46
问题 I'm creating a reporting engine that makes a couple of long queries over a standby server and process the result with pandas. Everything works fine but sometimes I have some issues with the execution of those queries using a psycopg2 cursor: the query is cancelled with the following message: ERROR: cancelling statement due to conflict with recovery Detail: User query might have needed to see row versions that must be removed I was investigating this issue PostgreSQL ERROR: canceling statement

Postegresql slow connect time on Windows

↘锁芯ラ 提交于 2021-01-27 04:27:28
问题 I noticed that the connection to PostgreSQL is pretty slow. import psycopg2 import time start_time = time.time() try: db = psycopg2.connect("dbname='xx' user='xxx' host='127.0.0.1' password='xxx' port='5433'") except Exception as e: print(e) exit(1) print('connect time', time.time() - start_time) Usual connect time is 2.5-3.5 seconds. connect time 3.3095390796661377 Its pretty much default configuration of freshly installed PostgreSQL. I turned off log_hostname but it changed nothing. I have

insertion of nested array of custom in table for postgres

核能气质少年 提交于 2021-01-07 01:23:45
问题 command = ( CREATE TYPE belongings AS ( item TEXT, quantity INTEGER ) CREATE TYPE student AS ( name TEXT, id INTEGER, bag belongings[] ) CREATE TABLE studentclass( date DATE NOT NULL, time TIMESTAMPTZ NOT NULL, PRIMARY KEY (date, time), class student ) ) Can i ask how to do insert for this in postgres psycog2? thank you. When i put the insert as insert_sql = "INSERT INTO studentclass (date, time, class) VALUES (%s,%s,%s)" error output is DETAIL: Cannot cast type text[] to belongings[] in