Questions tagged [psycopg2]

Psycopg is a PostgreSQL adapter for Python programming language. It implements PEP 249 with many extensions.

Psycopg is a PostgreSQL adapter for Python . It is a wrapper around the libpq library. Psycopg fully implements PEP 249. It is thread-safe. It supports asynchronous I/O, server-side cursors, COPY, large objects and two-phase commit. Psycopg is extensible with new adapters and typecasters.

Questions that relate directly to psycopg2 should receive this tag. If you are using a web framework, scientific library or ORM in conjunction with Psycopg2 be sure to include that information in your post, and consider adding tags for those libraries.

Psycopg can be easily installed on Linux or Mac, but lacks full support in Windows, though there is a port available. It requires some manual installation in a virtual environment.

Psycopg2 can be used to archive the state of objects, although it is often convenient to use an ORM (SQLAlchemy for example).

External Links

4351 questions
1
vote
0 answers

Psycopg2 install error on windows (pip or wheel) (UnicodeDecodeError)

I am currently trying to make use of pypy to try and see with a profiler if I will get better results than now, currently using only python 3.8 I have installed pypy 3.9 for windows 10 (64 bits) and been upgrading pip, setuptools, as well as wheels…
Charles L.
  • 13
  • 1
  • 5
1
vote
0 answers

Can't import from psycopg2 on Mac M1 Monterey 12.3.1

After installing psycopg2-binary==2.9.1 via pip install -r requirements.txt, when I run python manage.py runserver command locally in a django project I get the following output and error: main() File…
Gabe Naughton
  • 41
  • 1
  • 4
1
vote
1 answer

Context managers to connect to postgre with psycopg2

It is useful to connect using a context manager statements like this: with psycopg2.connect(**dns) as conn: with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs: ... How should I define a class to work with database using…
Ilya
  • 93
  • 6
1
vote
1 answer

Run psycopg version 3 on Lambda

I'm using the lambda docker base image for python3.9 FROM public.ecr.aws/lambda/python:3.9 And I'm trying to use psycopg in my code. Here is a minimum reproducible example: Dockerfile # This works! # FROM python:3.9 # This doesn't work FROM…
Leslie Alldridge
  • 1,427
  • 1
  • 10
  • 26
1
vote
0 answers

psycopg2 - how to detect broken but not closed connection?

I'm using psycopg2 ThreadedConnectionPool. When putting an already closed connection back in the pool using psycopg2's putconn, it doesn't get added to the pool, assuring that I won't be getting a closed connection next time I call get_conn. Is…
dawntime
  • 11
  • 3
1
vote
0 answers

Run PostgreSQL trigger as background

I'm trying to invoke a plpgsql function in an AWS RDS Postgres instance. Currently, I have an AFTER INSERT trigger to call the function I wanted to be executed. However this function takes a time to be executed, and the client keeps the connection…
1
vote
1 answer

psycopg2.InterfaceError: connection already closed on FastAPI

I followed the answer here to create a connection using psycopg2. It works on the first call on the endpoint. The second try gives this error psycopg2.InterfaceError: connection already closed. Below is a snippet of my code: from config import conn …
Fhuad Balogun
  • 47
  • 2
  • 8
1
vote
1 answer

Installation error psycopg2 termux Android

How can I install psycopg2 on my termux Android? I tried pip install psycopg2==2.2 pip3 install psycopg2-binary LDFLAGS="-L/system/lib64/" CFLAGS="-I/data/data/com.termux/files/usr/include/" pip install psycopg2-binary But everytime got this…
1
vote
1 answer

Export Postgresql Table to excel with header in Python

My code works but it doesn't bring the header with the names, it only brings the numbers 0 1 ... 10 , what can I do ? Utils_db.py def consulta_sql(sql): try: connection = psycopg2.connect(user="postgres", …
Fernando
  • 47
  • 5
1
vote
1 answer

How to pass psycopg2 cursor object to foreachPartition() in pyspark?

I'm getting following error Traceback (most recent call last): File "/databricks/spark/python/pyspark/serializers.py", line 473, in dumps return cloudpickle.dumps(obj, pickle_protocol) File…
1
vote
0 answers

Postgresql tables exists, but getting "relation does not exist" in Python

I am unable to query using psycopg2. DB connection is successful as DB version is printed. import psycopg2 import pandas.io.sql as psql cursor = connection.cursor() logger.info('PostgreSQL database version:') cursor.execute("SELECT…
Megha Dalvi
  • 91
  • 1
  • 1
  • 8
1
vote
1 answer

psycopg2 NotNullViolation on column "ip" for Django/Gunicorn/Nginx system

I have a Django system that has gunicorn hosting the dynamic content and nginx the static, nginx passing through to gunicorn as required via a Unix socket. Every request that requires static content is generating an error like that below. Is this…
Paul D Smith
  • 639
  • 5
  • 16
1
vote
1 answer

Why connection to Redshift from Python sometimes times out?

I'm connecting Redshift through Python from PC, Python from Lambda and SQL Workbench. SQL Workbech always connects without any issues. Both PC and Lambda often work correctly, but sometimes they can't connect - it looks like connection is waiting…
Manaslu
  • 228
  • 1
  • 13
1
vote
3 answers

how to pass asc desc as params in python psycopg2 raw sql execution?

i have a sample query template to accept dynamic parameters to execute: ps_conn = psycopg2.connect(...) ps_cursor = ps_conn.cursor() ps_cursor.execute(''' SELECT * FROM "posts" ) ORDER BY "posts"."rate" %s, "posts"."date_created" ASC…
1
vote
0 answers

psycop2 list of dictRow to nested pydantic objects

I am working on a small Python CRUD game using FastAPI. For personal reasons, I don't want to use an ORM and I am using psycop2 as a db connector and pydantic for schemas validation (for any CRUD operations). models/villages.py from pydantic import…
Pierre-Alexandre
  • 543
  • 1
  • 10
  • 31