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

Setting schema for all queries of a connection in psycopg2: Getting race condition when setting search_path

Our system is running on Ubuntu, python 3.4, postgres 9.4.x and psycopg2. We (will in the furture) split between dev, test and prod environments using schemas. I've create a convenience method for creating connections to our database. It uses json…
André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
24
votes
2 answers

psycopg2 : cursor already closed

I am using psycopg2 2.6.1. I have a bunch of queries that I need to execute in sequence. conn = psycopg2.connect(database=redshift_database, user=redshift_user, password=os.environ.get("PGPASSWORD"), …
nish
  • 6,952
  • 18
  • 74
  • 128
24
votes
4 answers

Operational Error: FATAL: database "django" does not exist

I'm a db dummy, and im trying to set up PostgreSQL for my django project. For that I also use psycopg2. Databases are complicated though. Personally I would like if there was a way to get hold of ALL my DATABASE- and USER-SETTINGS/INFO in one place.…
howtopythonpls
  • 770
  • 1
  • 6
  • 18
24
votes
4 answers

Getting error messages from psycopg2 exceptions

This is my first project using psycopg2 extensively. I'm trying to find a way to extract the psql error message for whenever a connection attempt fails. I've tested the code below will work if all the variables are set correctly, however whenever an…
sirjames2004
  • 453
  • 2
  • 9
  • 18
24
votes
4 answers

Python psycopg2 check row exists

In Python psycopg2 how can I check if a row exists? def track_exists(self, track_id): cur = self.conn.cursor() cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,)) if cur.fetchall() > 0: return…
André Ricardo
  • 3,051
  • 7
  • 23
  • 32
24
votes
3 answers

set transaction\query timeout in psycopg2?

Is there a way to set a timeout in psycopg2 for db transactions or for db queries? A sample use-case: Heroku limits django web requests to 30sec, after which Heroku terminates the request without allowing django to gracefully roll-back any…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
24
votes
6 answers

SQLAlchemy, Psycopg2 and Postgresql COPY

It looks like Psycopg has a custom command for executing a COPY: psycopg2 COPY using cursor.copy_from() freezes with large inputs Is there a way to access this functionality from with SQLAlchemy?
EoghanM
  • 25,161
  • 23
  • 90
  • 123
24
votes
1 answer

use try/except with psycopg2 or "with closing"?

I'm using Psycopg2 in Python to access a PostgreSQL database. I'm curious if it's safe to use the with closing() pattern to create and use a cursor, or if I should use an explicit try/except wrapped around the query. My question is concerning…
skyler
  • 8,010
  • 15
  • 46
  • 69
24
votes
2 answers

Psycopg2 reporting pg_hba.conf error

I've run into a weird situation while trying to use PostgreSQL and Psycopg2. For some reason, every time I attempt to connect to the postgre database via python, I get the following error: psycopg2.OperationalError: FATAL: no pg_hba.conf entry for…
Steve Gattuso
  • 7,644
  • 10
  • 44
  • 59
23
votes
2 answers

How to find psycopg2 version number

I installed psycopg2 on my Ubuntu Natty machine using apt-get. Now, I would like to know its version number. Can someone tell what the method to find version number for such python packages is.
Phani
  • 3,267
  • 4
  • 25
  • 50
23
votes
5 answers

Trouble installing psycopg2 on CentOS

I'm trying to install psycopg2 on CentOS, I followed everything on this tutorial from "On with it: Installing Python 2.6" all the way to when it imports psycopg2, but when I try to import I get the following error: Traceback (most recent call…
hdx
  • 4,198
  • 7
  • 26
  • 33
23
votes
3 answers

psycopg2 and SQL injection security

I am writing a class to be used as part of a much larger modeling algorithm. My part does spatial analysis to calculate distances from certain points to other points. There are a variety of conditions involving number of returned distances, cutoff…
wfgeo
  • 2,716
  • 4
  • 30
  • 51
23
votes
3 answers

How to setup django-hstore with an existing app managed by south?

I tried to use django-hstore using this nice tutorial. I added two classes to an existing app managed by South: class Attribute(models.Model): name = models.CharField(max_length=200, verbose_name=_("name")) description =…
Maxime R.
  • 9,621
  • 7
  • 53
  • 59
22
votes
1 answer

Error Saving geodjango PointField

I have a geo model with a PointField property. Everything works perfectly locally, but when I try to save an instance on the server, I get the following error: django.db.utils.DatabaseError: invalid byte sequence for encoding "UTF8": 0x00 I dug…
matthewwithanm
  • 3,733
  • 2
  • 21
  • 27
22
votes
6 answers

Install libpq-dev on Mac OS X

I'm trying to run Django with a Postgresql backend on my local Mac OS X. I've installed Django using pip: sudo pip install Django I've installed Postgresql with one of the binary installers here. But when I try to install psycopg2 I get an error…
tchaymore
  • 3,728
  • 13
  • 55
  • 86