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
36
votes
11 answers

Cannot install psycopg2 Ubuntu

Trying to get a server ready for a django project and I'm running into some issues with setup for postgres. I'm following this guide: https://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html And I'm at step 5: Now, we need to configure…
totaltotals
  • 703
  • 2
  • 7
  • 16
35
votes
6 answers

python postgres can I fetchall() 1 million rows?

I am using psycopg2 module in python to read from postgres database, I need to some operation on all rows in a column, that has more than 1 million rows. I would like to know would cur.fetchall() fail or cause my server to go down? (since my RAM…
Medya Gh
  • 4,563
  • 5
  • 23
  • 35
35
votes
4 answers

Psycopg2 on Amazon Elastic Beanstalk

I'm trying upload my project (in python) that uses Psycopg2 for Amazon Elastic Beanstalk. I'm doing this with a zip file containing my project and an requirements.txt file. But I'm receiving this error: Downloading/unpacking psycopg2>=2.4.6 (from…
Eduardo
  • 1,698
  • 4
  • 29
  • 48
34
votes
5 answers

Cannot install psycopg2 with pip3 on M1 Mac

I cannot install psycopg2 on my M1 Mac and I used pip3. When I tried to install with pip3 install psycopg2 The output like that: ERROR: Command errored out with exit status 1: command: /opt/homebrew/opt/python@3.9/bin/python3.9 -c 'import…
akasaa
  • 1,282
  • 4
  • 13
  • 33
33
votes
7 answers

Failing to install psycopg2-binary on new docker container

I have encountered a problem while trying to run my django project on a new Docker container. It is my first time using Docker and I can't seem to find a good way to run a django project on it. Having tried multiple tutorials, I always get the error…
kczan
  • 392
  • 1
  • 3
  • 9
33
votes
7 answers

Psycopg2 error: Symbol not found _PQbackendPID

I'm getting the following error from psycopg2: Traceback (most recent call last): File "test1.py", line 1, in import psycopg2 File "/Users/xxx/Library/Python/2.7/lib/python/site-packages/psycopg2/__init__.py", line 50, in
user5885331
  • 343
  • 1
  • 5
  • 10
33
votes
0 answers

psycopg2 could not translate host name

My airflow server periodically fails. When I check the gunicorn logs, the error before all works shutting down looks like this: OperationalError: (psycopg2.OperationalError) could not translate host name…
Brett
  • 719
  • 1
  • 10
  • 16
33
votes
3 answers

How do cursors work in Python's DB-API?

I have been using python with RDBMS' (MySQL and PostgreSQL), and I have noticed that I really do not understand how to use a cursor. Usually, one have his script connect to the DB via a client DB-API (like psycopg2 or MySQLdb): connection =…
Nicholas Leonard
  • 2,566
  • 4
  • 28
  • 32
33
votes
3 answers

psycopg2 leaking memory after large query

I'm running a large query in a python script against my postgres database using psycopg2 (I upgraded to version 2.5). After the query is finished, I close the cursor and connection, and even run gc, but the process still consumes a ton of memory…
32
votes
5 answers

psycopg2 equivalent of mysqldb.escape_string?

I'm passing some values into a postgres character field using psycopg2 in Python. Some of the string values contain periods, slashes, quotes etc. With MySQL I'd just escape the string with MySQLdb.escape_string(my_string) Is there an equivalent for…
AP257
  • 89,519
  • 86
  • 202
  • 261
32
votes
8 answers

copy data from csv to postgresql using python

I am on windows 7 64 bit. I have a csv file 'data.csv'. I want to import data to a postgresql table 'temp_unicommerce_status' via a python script. My Script is: import psycopg2 conn = psycopg2.connect("host='localhost' port='5432' dbname='Ekodev'…
Manish Gupta
  • 4,438
  • 18
  • 57
  • 104
29
votes
17 answers

Django 'pip install django-heroku'(psycopg2) error is blocking deployment to Heroku

I am setting up a new Django project to deploy on Heroku, however when I am following the Django Heroku deployment guide I come across an error during 'pip install django-heroku'. I am running on: OS: MacOS Mojave 10.14.6 virtualenv: python3 pip…
sudoxx
  • 423
  • 1
  • 4
  • 9
29
votes
4 answers

psycopg2: can't adapt type 'numpy.int64'

I have a dataframe with the dtypes shown below and I want to insert the dataframe into a postgres DB but it fails due to error can't adapt type 'numpy.int64' id_code int64 sector object created_date …
qboomerang
  • 1,931
  • 3
  • 15
  • 20
29
votes
10 answers

How to quote a string value explicitly (Python DB API/Psycopg2)

For some reasons, I would like to do an explicit quoting of a string value (becoming a part of constructed SQL query) instead of waiting for implicit quotation performed by cursor.execute method on contents of its second parameter. By "implicit…
Dariusz Walczak
  • 4,848
  • 5
  • 36
  • 39
29
votes
1 answer

Which Postgres value should I use in Django's DATABASE_ENGINE?

It's my first time using PostgreSQL 8.4.2 with Django(I have always used MySQL or sqlite3 in the past). Which value should I use for DATABASE_ENGINE in settings.py, postgresql_psycopg2 or postgresql? How do they differ from each other?
Thierry Lam
  • 45,304
  • 42
  • 117
  • 144