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
45
votes
8 answers

Insert Python Dictionary using Psycopg2

What is the best way to insert a Python dictionary with many keys into a Postgres database without having to enumerate all keys? I would like to do something like... song = dict() song['title'] = 'song 1' song['artist'] = 'artist…
user3783608
  • 857
  • 3
  • 11
  • 20
45
votes
8 answers

query from postgresql using python as dictionary

I'm using Python 2.7 and postgresql 9.1. Trying to get dictionary from query, I've tried the code as described here: http://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL import psycopg2 import psycopg2.extras conn =…
Guy Dafny
  • 1,709
  • 1
  • 14
  • 25
44
votes
8 answers

Getting the id of the last record inserted for Postgresql SERIAL KEY with Python

I am using SQLAlchemy without the ORM, i.e. using hand-crafted SQL statements to directly interact with the backend database. I am using PG as my backend database (psycopg2 as DB driver) in this instance - I don't know if that affects the answer. I…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
44
votes
10 answers

psycopg2 installation error - Library not loaded: libssl.dylib

I try to install psycopg2 in a virtualenv using pip, the compilation looks ok, it says "Successfully installed psycopg2", but when I try to import it in python interpreter (in virtualenv), it indicates error: File "", line 1, in
Yulong
  • 1,529
  • 1
  • 17
  • 26
43
votes
14 answers

Problems instaling libpq-dev in ubuntu 20.04

I am currently trying to install libpq-dev to install psycopg2. The problem is, when I try to install it, an error occurs saying I don't have the latest libpq5 version. However when I try to download the newer version of libpq5 the system says that…
lhmmendes
  • 433
  • 1
  • 4
  • 4
43
votes
9 answers

Unable to install psycopg2 (pip install psycopg2)

I'm using MAC and python version 2.7.14 Collecting psycopg2 Could not fetch URL https://pypi.python.org/simple/psycopg2/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version…
Rakesh Kumar
  • 547
  • 1
  • 4
  • 6
43
votes
1 answer

Python psycopg2 timeout

I have a huge problem: There seems to be some hardware problems on the router of the server my python software runs on. The connection to the database only is successfull about every third time. So a psycopg2.connect() can take up to 5 minutes…
gulden
  • 505
  • 1
  • 7
  • 13
41
votes
4 answers

How to mock psycopg2 cursor object?

I have this code segment in Python2: def super_cool_method(): con = psycopg2.connect(**connection_stuff) cur = con.cursor(cursor_factory=DictCursor) cur.execute("Super duper SQL query") rows = cur.fetchall() for row in rows: …
zyshara
  • 453
  • 1
  • 4
  • 7
41
votes
2 answers

ProgrammingError: relation "django_session" does not exist error after installing Psycopg2

I started to develop a Django based web application. Everything was fine until I installed Psycopg2 for my database which I created in PostgreSql. Now when I'm trying to open any page in my site, it throws ProgrammingError: relation "django_session"…
Ghasem
  • 14,455
  • 21
  • 138
  • 171
40
votes
2 answers

Use binary COPY table FROM with psycopg2

I have tens of millions of rows to transfer from multidimensional array files into a PostgreSQL database. My tools are Python and psycopg2. The most efficient way to bulk instert data is using copy_from. However, my data are mostly 32-bit floating…
Mike T
  • 41,085
  • 18
  • 152
  • 203
40
votes
4 answers

Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo"

I am getting problem in Django project setting with POSTGRESQL. Here is my setting.py database setting DATABASES = { 'default':{ 'ENGINE':'django.db.backends.postgresql_psycopg2', # '.postgresql_psycopg2', '.mysql', or '.oracle' …
masterofdestiny
  • 2,751
  • 11
  • 28
  • 40
39
votes
4 answers

Install pip on pypy

I want to speed up my program so i'm trying to setup pypy + psycopg2cffi. This program opens a xml, parses it and then insert some data in a database. I'm using currently python3, postgresql and psycopg2 but this approaches is really slow. So i want…
1pa
  • 715
  • 2
  • 9
  • 23
38
votes
12 answers

No module named 'psycopg2._psycopg': ModuleNotFoundError in AWS Lambda

I have created a deployment package for AWS Lambda with my python file and the dependencies including sqlalchemy and psycopg2. The code works perfectly in accessing the DB locally. But when I imported this zip file, I am getting the following error.…
Kathiravan Natarajan
  • 3,158
  • 6
  • 22
  • 45
38
votes
7 answers

How to write data to Redshift that is a result of a dataframe created in Python?

I have a dataframe in Python. Can I write this data to Redshift as a new table? I have successfully created a db connection to Redshift and am able to execute simple sql queries. Now I need to write a dataframe to it.
Sahil
  • 413
  • 2
  • 5
  • 8
38
votes
12 answers

ImportError: No module named 'psycopg2._psycopg'

When I try to import psycopg2 it show below log for me: Traceback (most recent call last): File "D:/Desktop/learn/python/webcatch/appserver/testpgsql.py", line 2, in import psycopg2 File…
sappy
  • 770
  • 2
  • 6
  • 16