Psycopg is a popular PostgreSQL adapter for the Python programming language. At its core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.
Questions tagged [psycopg]
59 questions
29
votes
3 answers
Psycopg2 Insert Into Table with Placeholders
This might be a rather silly question but what am I doing wrong here? It creates the table but the INSERT INTO doesn't work, I guess I'm doing something wrong with the placeholders?
conn = psycopg2.connect("dbname=postgres user=postgres")
cur =…

Timothy Dalton
- 1,290
- 2
- 17
- 24
18
votes
3 answers
psycopg - Get formatted sql instead of executing
I have a piece of Python code, that interacts with a PostgreSQL database via psycopg.
All literature warns against doing sql formatting by oneself, and recommends letting the driver do it. E.g.:
cur.execute('select name, age from people where name…

Erik Ninn-Hansen
- 565
- 2
- 7
- 16
17
votes
3 answers
How can I pool connections using psycopg and gevent?
The psycopg docs state: "Psycopg connections are not green thread safe and can’t be used concurrently by different green threads. Trying to execute more than one command at time using one cursor per thread will result in an error (or a deadlock on…

gone
- 4,342
- 2
- 24
- 29
15
votes
2 answers
How do I know if I have successfully created a table (Python, Psycopg2)?
I've looked at the documentations but haven't found anything that lets me know if the last command i've execute via cursor.execute("...") is successful.
I'm expecting a reply like "1 row affected."

kev
- 1,085
- 2
- 10
- 27
15
votes
4 answers
python adds "E" to string
This string:
"CREATE USER %s PASSWORD %s", (user, pw)
always gets expanded to:
CREATE USER E'someuser' PASSWORD E'somepassword'
Can anyone tell me why?
Edit:
The expanded string above is the string my database gives me back in the error message.…

Kai
- 2,205
- 3
- 32
- 43
15
votes
1 answer
psycopg2 error: DatabaseError: error with no message from the libpq
I have an application that parses and loads data from csv files into a Postgres 9.3 database. In serial execution insert statements/cursor executions work without an issue.
I added celery in the mix to add parallel parsing and inserting of the data…

user982599
- 975
- 13
- 28
14
votes
3 answers
How to use "INSERT" in psycopg2 connection pooling?
I use psycopg2 to connect to PostgreSQL on Python and I want to use connection pooling.
I don't know what should I do instead commit() and rollback() when I execute INSERT query.
db = pool.SimpleConnectionPool(1,…

Foad Tahmasebi
- 1,333
- 4
- 16
- 33
7
votes
3 answers
prepared statements using psycopg
I'm a beginner at python. We use this code to execute SQL commands.
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abcdef"))
I wonder is this prepared statement or just a client side quoting?

Majid Azimi
- 5,575
- 13
- 64
- 113
6
votes
3 answers
PostgreSQL connection closes unexpectedly when doing a large insert
I'm populating a PostgreSQL table with ~11.000.000 rows that have been selected before from another database. I'm using Python and psycopg2. The whole process takes an estimated 1.5 hours to complete. However, after ~30 minutes I get "connection…

WolfgangP
- 3,195
- 27
- 37
6
votes
4 answers
Python-PostgreSQL psycopg2 interface --> executemany
I am currently analyzing a wikipedia dump file; I am extracting a bunch of data from it using python and persisting it into a PostgreSQL db. I am always trying to make things go faster for this file is huge (18GB). In order to interface with…
Nicholas Leonard
5
votes
1 answer
Is postgres caching my query?
I have a pretty simple snippet of Python code to run a Postgres query then send the results to a dashboard. I'm using psycopg2 to periodically run the same query. Let's not worry about the looping mechanism for now.
conn =…

s g
- 5,289
- 10
- 49
- 82
5
votes
1 answer
TypeError: 'dict' object does not support indexing thrown on second instance of this query
So I am building a query based on user input in flask with this code:
if empty_indic_form.validate_on_submit():
query='select name, year, value, display_name from literal inner join ent on ent_id=ent.id where display_name in ('
for i in…

Vincent Buscarello
- 395
- 2
- 7
- 19
5
votes
1 answer
Proper use of serialization with psycopg2
I am accessing a postgresql table with serialization transaction isolation. I am doing something like this (with an existing psycopg2 connection conn and cursor in that connection, cur:
while True:
try:
cur.execute(query)
break
except…

Ken - Enough about Monica
- 1,595
- 4
- 21
- 50
5
votes
2 answers
What is the equivalent of psycopg curs.mogrify on mysql?
What would be the equivalent of psycopg's cur.mogrify on mysql?
From: http://initd.org/psycopg/docs/cursor.html
mogrify(operation[, parameters])
Return a query string after arguments binding. The string returned is exactly the one that would be…

thclpr
- 5,778
- 10
- 54
- 87
5
votes
2 answers
cursort.execute/cursor.callproc returns no error but nothing is executed
When i run this SQL query via psql client it runs for several seconds (~90 seconds, that's normal since its a huge table) and it return, then i can check that my line is successfully inserted.
SELECT merge_data('898989', '111111111', '10000')
It is…

zfou
- 891
- 1
- 10
- 33