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
108
votes
19 answers

Can't install psycopg2 package through pip install on MacOS

I am working on a project for one of my lectures and I need to download the package psycopg2 in order to work with the postgresql database in use. Unfortunately, when I try to pip install psycopg2 the following error pops up: ld: library not found…
Jome
  • 1,285
  • 2
  • 11
  • 20
97
votes
27 answers

Error Installing Psycopg2 on MacOS 10.9.5

I'm trying to install Psycopg2 on my Macbook, but I am getting an error. I found a lot of the same questions on StackOverflow but no answer seems to work. I'm using: OS: MacOS 10.9.5 Python Version: 3.4.3 My error code is: Running setup.py…
sg_sg94
  • 2,248
  • 4
  • 28
  • 56
97
votes
12 answers

Error trying to install Postgres for python (psycopg2)

I tried to install psycopg2 to my environment, but I get the following error: (venv)avlahop@apostolos-laptop:~/development/django/rhombus-dental$ sudo pip install psycopg2 Downloading/unpacking psycopg2, Downloading psycopg2-2.5.2.tar.gz (685kB):…
Apostolos
  • 7,763
  • 17
  • 80
  • 150
95
votes
2 answers

How do you get PyPy, Django and PostgreSQL to work together?

What fork, or combination of packages should one to use to make PyPy, Django and PostgreSQL play nice together? I know that PyPy and Django play nice together, but I am less certain about PyPy and PostgreSQL. I do see that Alex Gaynor has made a…
James R
  • 4,571
  • 3
  • 30
  • 45
93
votes
7 answers

How do I get tables in postgres using psycopg2?

Can someone please explain how I can get the tables in the current database? I am using postgresql-8.4 psycopg2.
user1395784
  • 931
  • 1
  • 6
  • 3
91
votes
2 answers

Pandas read_sql with parameters

Are there any examples of how to pass parameters with an SQL query in Pandas? In particular I'm using an SQLAlchemy engine to connect to a PostgreSQL database. So far I've found that the following works: df = psql.read_sql(('select…
tobycoleman
  • 1,664
  • 1
  • 18
  • 34
89
votes
5 answers

How do I check if a json key exists in Postgres?

Let's say I have a json that looks like this: some_json = {'key_a': {'nested_key': 'a'}, 'key_b': {'nested_key': 'b'}} Note that key_a and key_b are optional keys mapped to dictionaries and may or may not exist. I have a function that…
Teboto
  • 1,005
  • 1
  • 12
  • 16
86
votes
19 answers

"Failed building wheel for psycopg2" - MacOSX using virtualenv and pip

I'm attempting to make a website with a few others for the first time, and have run into a weird error when trying to use Django/Python/VirtualEnv. I've found solutions to this problem for other operating systems, such as Ubuntu, but can't find any…
Taygo0o
  • 861
  • 1
  • 6
  • 3
84
votes
10 answers

Passing table name as a parameter in psycopg2

I have the following code, using pscyopg2: sql = 'select %s from %s where utctime > %s and utctime < %s order by utctime asc;' data = (dataItems, voyage, dateRangeLower, dateRangeUpper) rows = cur.mogrify(sql, data) This outputs: select 'waterTemp,…
Caligari
  • 1,381
  • 2
  • 10
  • 14
83
votes
23 answers

Psycopg2 image not found

Trying to setup postgres with the postgres mac app and hit this error, which I haven't been able to solve. Any thoughts? ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded:…
Craig Cannon
  • 1,449
  • 2
  • 13
  • 20
81
votes
16 answers

django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg

Presently, I'm attempting to configure Django for use on a project, and am encountering a persistent error when I try to run python manage.py syncdb. File "/x/x/x/x/x/x/base.py", line 23, in ? raise ImproperlyConfigured("Error loading psycopg…
user968270
  • 4,251
  • 5
  • 21
  • 20
79
votes
15 answers

Installing psycopg2 into virtualenv when PostgreSQL is not installed on development system

Is it possible to install psycopg2 into a virtualenv when PostgreSQL isn't installed on my development system—MacBook Pro with OS X 10.6? When I run pip install psycopg2 from within my virtualenv, I received the error shown below. I'm trying to…
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
79
votes
15 answers

How to install psycopg2 with pg_config error?

I've tried to install psycopg2 (PostgreSQL Database adapater) from this site, but when I try to install after I cd into the package and write python setup.py install I get the following error: Please add the directory containing pg_config to the…
Chris
  • 5,444
  • 16
  • 63
  • 119
77
votes
6 answers

DictCursor doesn't seem to work under psycopg2

I haven't worked with psycopg2 before but I'm trying to change the cursor factory to DictCursor so that fetchall or fetchone will return a dictionary instead of a list. I created a test script to make things simple and only test this functionality.…
Jim
  • 1,604
  • 1
  • 14
  • 17
77
votes
3 answers

Create a Postgres database using python

I want to create Postgres database using Python. con = psql.connect(dbname='postgres', user=self.user_name, host='', password=self.password) cur = con.cursor() cur.execute("CREATE DATABASE %s ;" % self.db_name) I am getting the…
kiran6
  • 1,247
  • 2
  • 13
  • 19