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
21
votes
2 answers

Graceful Primary Key Error handling in Python/psycopg2

Using Python 2.7 and In [150]: psycopg2.version Out[150]: '2.4.2 (dt dec pq3 ext)' I have a simple python scripts that processing transactions and writes data to a database. Occasionally there is an insert that violates my primary key. This is…
jdennison
  • 2,000
  • 2
  • 15
  • 22
21
votes
4 answers

Where can I download binary eggs with psycopg2 for Windows?

I'm looking for binary eggs with psycopg2's binaries for Windows but can't find any. On http://initd.org/psycopg/download/ there's only source package and link to Windows port of Psycopg which provides binary installers but no binary eggs. The…
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
21
votes
2 answers

Pass column name as parameter to PostgreSQL using psycopg2

I'm trying to add columns to a table using psycopg2 row1 below is a list of column names to be added to the table. I can do it manually but when I try to do it programatically I get an error. for c in row1: cur.execute("ALTER TABLE HHV2PUB ADD…
nodebase
  • 2,510
  • 6
  • 31
  • 46
21
votes
4 answers

How do I install psycopg2 for Python 3.x?

Just started Python a few days ago and I'm using PyCharm to develop a web application with Django. I have libpq-dev python-dev packages already installed, but it's still throwing me the same error: ./psycopg/psycopg.h:30:20: fatal error: Python.h:…
Lunyx
  • 3,164
  • 6
  • 30
  • 46
21
votes
4 answers

psycopg2 not returning results

I am trying to use psycopg2 with my postgresql database just running on my local machine can't get it to return results no matter what I try. It seems to connect to the database ok, since if I alter any of the config parameters it throws errors,…
bqui56
  • 2,091
  • 8
  • 22
  • 40
20
votes
6 answers

How can I speed up update/replace operations in PostgreSQL?

We have a rather specific application that uses PostgreSQL 8.3 as a storage backend (using Python and psycopg2). The operations we perform to the important tables are in the majority of cases inserts or updates (rarely deletes or selects). For…
Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60
20
votes
1 answer

pip install psycopg2 - error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

I get the following error when trying to install psycopg2: (venv) root@scw-determined-panini:/app# pip install psycopg2 Collecting psycopg2 Using cached…
Nikk
  • 7,384
  • 8
  • 44
  • 90
20
votes
3 answers

Error when creating a PostgreSQL database using python, sqlalchemy and psycopg2

I use sqlalchemy that uses psycopg2 for connecting to postgresql servers. When I launch the following code: from sqlalchemy.engine.url import URL from sqlalchemy.engine import create_engine url = URL(drivername='postgresql', username='myname',…
Roman Prykhodchenko
  • 12,655
  • 8
  • 29
  • 33
20
votes
4 answers

Exporting a PostgreSQL query to a csv file using Python

I need to export some rows from a table in a PostgreSQL database to a .csv file using a Python script: #!/usr/bin/python # -*- coding: utf-8 -*- import sys, psycopg2 ... conn = psycopg2.connect("dbname=dbname user=user password=password") …
Raúl Casado
  • 293
  • 1
  • 2
  • 13
20
votes
1 answer

Error: could not determine PostgreSQL version from '10.3' - Django on Heroku

I tried to push from local env to Heroku master. No new requirements from the previous commit. However, I received an Error which saying the system could not determine PostgreSQL version from "10.3". Here is my requirements…
aluk.e
  • 253
  • 1
  • 2
  • 9
20
votes
4 answers

Failure to connect to Docker Postgresql instance from Python

I am using Docker to "containerize" a PostgreSQL deployment. I can spin up the container and connect to PostgreSQL via the command line as shown below: minime2@CEBERUS:~/Projects/skunkworks$ docker ps CONTAINER ID IMAGE COMMAND …
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
20
votes
4 answers

Psycopg2 peer authentication for user postgres

I seem to have correctly installed PostgreSQL 9.5.5. and Psycopg2 on Ubuntu 16.04, and can log in via: sudo -u postgres psql If I then issue \conninfo, I get the following: You are connected to database "postgres" as user "postgres" via socket in…
Yuri Lifanov
  • 347
  • 1
  • 2
  • 11
20
votes
7 answers

Psycopg2 Python SSL Support is not compiled in

I am trying to connect to my postgres database using psycopg2 with sslmode='required' param; however, I get the following error psycopg2.OperationalError: sslmode value "require" invalid when SSL support is not compiled in Heres a couple details…
user2158382
  • 4,430
  • 12
  • 55
  • 97
20
votes
1 answer

Is it required to close a Psycopg2 connection at the end of a script?

What are the consequences of not closing a psycopg2 connection at the end of a Python script? For example, consider the following snippet: import psycopg2 psycopg2.connect("dbname=test") The script opens a connection, but does not close it at the…
Marco
  • 3,053
  • 5
  • 27
  • 29
20
votes
1 answer

Recreating Postgres COPY directly in Python?

I have a block of data, currently as a list of n-tuples but the format is pretty flexible, that I'd like to append to a Postgres table - in this case, each n-tuple corresponds to a row in the DB. What I had been doing up to this point is writing…
geoffjentry
  • 4,674
  • 3
  • 31
  • 37