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
59
votes
4 answers

How to insert 'NULL' values into PostgreSQL database using Python?

Is there a good practice for entering NULL key values to a PostgreSQL database when a variable is None in Python? Running this query: mycursor.execute('INSERT INTO products (user_id, city_id, product_id, quantity, price) VALUES (%i, %i, %i, %i, %f)'…
xpanta
  • 8,124
  • 15
  • 60
  • 104
57
votes
11 answers

sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver

I am trying to run alembic migration and when I run alembic revision --autogenerate -m "Added initial tables" It fails saying sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver the database url is…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
57
votes
4 answers

Connect to an URI in postgres

I'm guessing this is a pretty basic question, but I can't figure out why: import psycopg2 psycopg2.connect("postgresql://postgres:postgres@localhost/postgres") Is giving the following error: psycopg2.OperationalError: missing "="…
Daan Bakker
  • 6,122
  • 3
  • 24
  • 23
57
votes
5 answers

Making sure that psycopg2 database connection alive

I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won't work with OperationalError exception. So I'm looking for…
HardQuestions
  • 4,075
  • 7
  • 34
  • 39
56
votes
9 answers

Can't connect the postgreSQL with psycopg2

It's the first time that I can't find the answer about some tech problems Here's my problems: >> conn=psycopg2.connect(database="mydb", user="postgres", password="123",port=5432) Traceback (most recent call last): File "", line 1, in…
Lanston
  • 11,354
  • 8
  • 32
  • 37
55
votes
13 answers

Escape SQL "LIKE" value for Postgres with psycopg2

Does psycopg2 have a function for escaping the value of a LIKE operand for Postgres? For example I may want to match strings that start with the string "20% of all", so I want to write something like this: sql = '... WHERE ... LIKE…
EMP
  • 59,148
  • 53
  • 164
  • 220
54
votes
9 answers

How to set connection timeout in SQLAlchemy

I'm trying to figure out how to set the connection timeout in create_engine(), so far I've tried: create_engine(url, timeout=10) TypeError: Invalid argument(s) 'timeout' sent to create_engine(), using configuration…
daveoncode
  • 18,900
  • 15
  • 104
  • 159
52
votes
5 answers

connect to a DB using psycopg2 without password

I have a postgres database on my localhost I can access without a password $ psql -d mwt psql (8.4.12) Type "help" for help. mwt=# SELECT * from vatid; id | requester_vatid |... -----+-----------------|... 1719 | IT00766780266 |... I…
Pablo
  • 13,271
  • 4
  • 39
  • 59
48
votes
1 answer

Using INSERT with a PostgreSQL Database using Python

I am trying to insert data into a PostgreSQL database table using Python. I don't see any syntax errors but, for some reason, my data isn't getting inserted into the database. conn = psycopg2.connect(connection) cursor = conn.cursor() items =…
Ching Chong
  • 723
  • 1
  • 8
  • 20
48
votes
4 answers

Improving Postgres psycopg2 query performance for Python to the same level of Java's JDBC driver

Overview I'm attempting to improve the performance of our database queries for SQLAlchemy. We're using psycopg2. In our production system, we're chosing to go with Java because it is simply faster by at least 50%, if not closer to 100%. So I am…
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55
48
votes
8 answers

Problems using psycopg2 on Mac OS (Yosemite)

Currently I am installing psycopg2 for work within eclipse with python. I am finding a lot of problems: The first problem sudo pip3.4 install psycopg2 is not working and it is showing the following message Error: pg_config executable not…
Benja Garrido
  • 701
  • 1
  • 5
  • 17
48
votes
1 answer

Creating a postgresql DB using psycopg2

I'm trying to create a postgres DB using a python script. Some research showed that using the psycopg2 module might be a way to do it. I installed it and made the required changes in the pg_hba.conf file. I used the following code to create the…
nish
  • 6,952
  • 18
  • 74
  • 128
47
votes
9 answers

Psycopg2, Postgresql, Python: Fastest way to bulk-insert

I'm looking for the most efficient way to bulk-insert some millions of tuples into a database. I'm using Python, PostgreSQL and psycopg2. I have created a long list of tulpes that should be inserted to the database, sometimes with modifiers like…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
47
votes
2 answers

Execute .sql schema in psycopg2 in Python

I have a PostgreSQL schema stored in .sql file. It looks something like: CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, facebook_id TEXT NOT NULL, name TEXT NOT NULL, access_token TEXT, created INTEGER NOT…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
46
votes
8 answers

PostgreSQL - how to run VACUUM from code outside transaction block?

I am using Python with psycopg2 and I'm trying to run a full VACUUM after a daily operation which inserts several thousand rows. The problem is that when I try to run the VACUUM command within my code I get the following…
Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72