psycopg2

Close an existing Postgres connection using psycopg2

别说谁变了你拦得住时间么 提交于 2021-01-29 16:25:15
问题 I was playing around with Postgresql and psycopg2 . I think I started many connections using the terminal but never closed it. Using pyscopg2 I understood how to start a connection and close it too. Now I was trying to get the existing connection (that i launched using the terminal before) using pyscopg2 but it seems there is an issue with port number. When I run SELECT * FROM pg_stat_activity ; , these are my results datid | datname | pid | usesysid | usename | application_name | client_addr

Python For Loop Slows Due To Large List

戏子无情 提交于 2021-01-29 12:52:10
问题 So currently I have a for loop, which causes the python program to die with the program saying 'Killed'. It slows down around 6000 items in, with the program slowly dying at around 6852 list items. How do I fix this? I assume it's due to the list being too large. I've tried splitting the list in two around 6000. Maybe it's due to memory management or something. Help would be appreciated. for id in listofids: connection = psycopg2.connect(user = "username", password = "password", host =

Create table from dictionary data in a safe way

穿精又带淫゛_ 提交于 2021-01-29 05:43:18
问题 I have a problem where i have a list of dictionaries with for example the following data: columns = [{ 'name': 'column1', 'type': 'varchar' }, { 'name': 'column2', 'type': 'decimal' }, . . . ] From that list i need to dynamically create a CREATE TABLE statement based on each dictionary in the list which contains the name of the column and the type and execute it on a PostgreSQL database using the psycopg2 adapter. I managed to do it with: columns = "(" + ",\n".join(["{} {}".format(col['name']

Unable to insert nested record in postgres

∥☆過路亽.° 提交于 2021-01-29 05:40:16
问题 i had managed to create tables in postgres but encountered issues when trying to insert values. comands = ( CREATE TYPE student AS ( name TEXT, id INTEGER ) CREATE TABLE studentclass( date DATE NOT NULL, time TIMESTAMPTZ NOT NULL, PRIMARY KEY (date, time), class student ) ) And in psycog2 command = ( INSERT INTO studentclass (date, time, student) VALUES (%s,%s, ROW(%s,%s)::student) ) student_rec = ("John", 1) record_to_insert = ("2020-05-21", "2020-05-21 08:10:00", student_rec) cursor.execute

From psycopg2 import sql not working

回眸只為那壹抹淺笑 提交于 2021-01-29 04:44:25
问题 Looking for some help with an issue I am having with psycopg2 and Python3. I am trying to use the sql library from psycopg2 that is available in version 2.7.1. When trying to use from psycopg2 import sql I get the error message unresolved reference 'sql' I am using Xubuntu Linux 16.04 LTS and when issuing the command pip show psycopg2 in the terminal I get: Name: psycopg2 Version: 2.7.1 Summary: psycopg2 - Python-PostgreSQL Database Adapter Home-page: http://initd.org/psycopg/ Author:

From psycopg2 import sql not working

馋奶兔 提交于 2021-01-29 04:40:49
问题 Looking for some help with an issue I am having with psycopg2 and Python3. I am trying to use the sql library from psycopg2 that is available in version 2.7.1. When trying to use from psycopg2 import sql I get the error message unresolved reference 'sql' I am using Xubuntu Linux 16.04 LTS and when issuing the command pip show psycopg2 in the terminal I get: Name: psycopg2 Version: 2.7.1 Summary: psycopg2 - Python-PostgreSQL Database Adapter Home-page: http://initd.org/psycopg/ Author:

Python psql \copy CSV to remote server

人走茶凉 提交于 2021-01-28 13:02:11
问题 I am attempting to copy a csv (which has a header and quote character ") with python 3.6 to a table on a remote postgres 10 server. It is a large CSV (2.5M rows, 800MB) and while I previously imported it into a dataframe and then used dataframe.to_sql, this was very memory intensive so I switched to using COPY. Using COPY with psycopg2 or sqlalchemy would work fine but the remote server does not have access to the local file system. Using psql in the terminal I have successfully run the query

Python psql \copy CSV to remote server

左心房为你撑大大i 提交于 2021-01-28 13:01:37
问题 I am attempting to copy a csv (which has a header and quote character ") with python 3.6 to a table on a remote postgres 10 server. It is a large CSV (2.5M rows, 800MB) and while I previously imported it into a dataframe and then used dataframe.to_sql, this was very memory intensive so I switched to using COPY. Using COPY with psycopg2 or sqlalchemy would work fine but the remote server does not have access to the local file system. Using psql in the terminal I have successfully run the query

ERROR: Failed building wheel for psycopg2 (Ubuntu 20.04 + Python 3.8.5 + venv)

拟墨画扇 提交于 2021-01-28 11:25:53
问题 Greetings wisdom from Stackoverflow! I'm having issues building wheel for psycopg2 thru pip install -r requirements.txt . I'm on ubuntu 20.04 + python 3.8.5 + venv. This is my requirements.txt: amqp==2.6.1 anyjson==0.3.3 asgiref==3.2.10 billiard==3.6.3.0 brotlipy==0.7.0 celery==4.4.7 celery-progress==0.0.12 certifi==2020.6.20 cffi==1.14.2 chardet==3.0.4 cryptography==3.1 Django==3.0.3 dj-database-url==0.5.0 django-celery-results==1.2.1 django-cors-headers==3.5.0 django-crispy-forms==1.9.2

SQL identifier substitution - Using a list of column names

余生长醉 提交于 2021-01-28 07:40:40
问题 I am trying to run a query of the form: SELECT {} from TABLE where foo = VALUE But I want to be able to provide a list to replace the {} According to the psycopg docs, in order to do this safely, you need to use the sql.Identifier function, in order to properly escape the parameter, and then do something like this: SQL = sql.SQL( "SELECT {} FROM foo WHERE bar = %s" ).format(identifier) cursor.execute(SQL, [VALUE]) This works, when identifier is a single element, but I need it to be an