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
1420
votes
57 answers

pg_config executable not found

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2: Error: pg_config executable not found. Please add the directory containing pg_config to the PATH or specify the full executable path with the…
user1448207
  • 14,335
  • 3
  • 14
  • 5
702
votes
36 answers

How to install psycopg2 with "pip" on Python?

I'm using virtualenv and I need to install "psycopg2". I have done the following: pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160 And I have the following…
André
  • 24,706
  • 43
  • 121
  • 178
355
votes
21 answers

DatabaseError: current transaction is aborted, commands ignored until end of transaction block?

I got a lot of errors with the message : "DatabaseError: current transaction is aborted, commands ignored until end of transaction block" after changed from python-psycopg to python-psycopg2 as Django project's database engine. The code remains…
jack
  • 17,261
  • 37
  • 100
  • 125
266
votes
3 answers

SQLAlchemy: engine, connection and session difference

I use SQLAlchemy and there are at least three entities: engine, session and connection, which have execute method, so if I e.g. want to select all records from table I can do this engine.execute(select([table])).fetchall() and…
ololobus
  • 3,568
  • 2
  • 18
  • 23
231
votes
19 answers

psycopg2: insert multiple rows with one query

I need to insert multiple rows with one query (number of rows is not constant), so I need to execute query like this one: INSERT INTO t (a, b) VALUES (1, 2), (3, 4), (5, 6); The only way I know is args = [(1,2), (3,4), (5,6)] args_str =…
Sergey Fedoseev
  • 2,800
  • 2
  • 18
  • 16
222
votes
11 answers

How do I get a list of column names from a psycopg2 cursor?

I would like a general way to generate column labels directly from the selected column names, and recall seeing that python's psycopg2 module supports this feature.
Setjmp
  • 27,279
  • 27
  • 74
  • 92
209
votes
8 answers

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

I'm trying to connect to a Postgres database with SQLAlchemy. I've installed psycopg2. However, I get the error sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres. How do I configure SQLAlchemy to connect to…
Maharsh Gheewala
  • 2,091
  • 2
  • 10
  • 6
203
votes
25 answers

error installing psycopg2, library not found for -lssl

I run sudo pip install psycopg2 and I get a bunch of output that looks like: cc -DNDEBUG -g -fwrapv -Os ..... ..... cc -DNDEBUG -g -fwrapv -Os ..... ..... And at the end it says: ld: library not found for -lssl clang: error: linker command…
tscizzle
  • 11,191
  • 15
  • 54
  • 88
169
votes
5 answers

Python/postgres/psycopg2: getting ID of row just inserted

I'm using Python and psycopg2 to interface to postgres. When I insert a row... sql_string = "INSERT INTO hundred (name,name_slug,status) VALUES (" sql_string += hundred_name + ", '" + hundred_slug + "', " + status +…
AP257
  • 89,519
  • 86
  • 202
  • 261
127
votes
5 answers

Python/psycopg2 WHERE IN statement

What is the correct method to have the list (countryList) be available via %s in the SQL statement? # using psycopg2 countryList=['UK','France'] sql='SELECT * from countries WHERE country IN (%s)' data=[countryList] cur.execute(sql,data) As it is…
Matt
  • 7,022
  • 16
  • 53
  • 66
124
votes
13 answers

Mac + virtualenv + pip + postgresql = Error: pg_config executable not found

I was trying to install postgres for a tutorial, but pip gives me error: pip install psycopg A snip of error I get: Error: pg_config executable not found. Please add the directory containing pg_config to the PATH or specify the full executable…
KGo
  • 18,536
  • 11
  • 31
  • 47
120
votes
21 answers

Error: No module named psycopg2.extensions

I am trying to set up a PostgreSQL database for my django project, which I believe I have done now thanks to the replies to my last question Problems setting up a postgreSQL database for a django project. I am now trying to run the command 'python…
Jess
  • 1,749
  • 4
  • 16
  • 17
116
votes
13 answers

How to set up a PostgreSQL database in Django

I'm new to Python and Django. I'm configuring a Django project using a PostgreSQL database engine backend, But I'm getting errors on each database operation. For example when I run manage.py syncdb, I'm getting: C:\xampp\htdocs\djangodir>python…
André
  • 24,706
  • 43
  • 121
  • 178
115
votes
5 answers

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker

I'm trying to build a Flask app using Postgres with Docker. I'd like to connect to an AWS RDS instance of Postgres, but use Docker for my Flask app. However, when trying to set up psycopg2 it runs into an error because it can't find pg_config.…
dace
  • 5,819
  • 13
  • 44
  • 74
113
votes
2 answers

psycopg2: AttributeError: 'module' object has no attribute 'extras'

In my code I use the DictCursor from psycopg2.extras like this dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) However, all of the sudden I get the following error when I load the cursor: AttributeError: 'module' object has no…
n1000
  • 5,058
  • 10
  • 37
  • 65
1
2 3
99 100