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

psycopg2 not actually inserting data

I need to insert JSON data from tornado to postgres, so here's test like this: from psycopg2 import connect conn = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'") cursor = conn.cursor() data =…
juk
  • 2,179
  • 4
  • 19
  • 25
74
votes
14 answers

Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7

I am following Heroku's tutorial to deploy a Django app: http://devcenter.heroku.com/articles/django#prerequisites. Everything is working fine until I get to this part: $ pip install Django psycopg2 I can install Django by itself, but the probelm…
WarAndPiece
  • 1,007
  • 1
  • 10
  • 10
70
votes
2 answers

SQLAlchemy or psycopg2?

I am writing a quick and dirty script which requires interaction with a database (PG). The script is a pragmatic, tactical solution to an existing problem. however, I envisage that the script will evolve over time into a more "refined" system. Given…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
70
votes
7 answers

psycopg2 TypeError: not all arguments converted during string formatting

I'm trying execute a simple query, but getting this error no matter how I pass the parameters. Here is the query (I'm using Trac db object to connect to a DB): cursor.execute("""SELECT name FROM "%s".customer WHERE firm_id='%s'""" % (schema,…
konart
  • 1,714
  • 1
  • 12
  • 19
68
votes
7 answers

Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2

In one folder I have 3 files: base.py, Dockerfile and docker-compose.yml. base.py: import psycopg2 conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'") Dockerfile: FROM ubuntu:16.04 RUN apt-get update RUN…
gongarek
  • 984
  • 1
  • 8
  • 19
67
votes
4 answers

Parameterized queries with psycopg2 / Python DB-API and PostgreSQL

What's the best way to make psycopg2 pass parameterized queries to PostgreSQL? I don't want to write my own escpaing mechanisms or adapters and the psycopg2 source code and examples are difficult to read in a web browser. If I need to switch to…
jeffcook2150
  • 4,028
  • 4
  • 39
  • 51
66
votes
2 answers

TypeError: Object of type 'DataFrame' is not JSON serializable

I'm trying to create a plotly graph with some data I've got from my PostgreSQL server, but when I try to graph I'm getting an error: "TypeError: Object of type 'DataFrame' is not JSON serializable" Here's the code so far: import dash import numpy as…
peolss
  • 663
  • 1
  • 5
  • 5
66
votes
8 answers

Postgres SSL SYSCALL error: EOF detected with python and psycopg

Using psycopg2 package with python 2.7 I keep getting the titled error: psycopg2.DatabaseError: SSL SYSCALL error: EOF detected It only occurs when I add a WHERE column LIKE ''%X%'' clause to my pgrouting query. An example: SELECT id1 as node, cost…
Phil Donovan
  • 1,075
  • 1
  • 9
  • 18
65
votes
1 answer

Getting affected row count from psycopg2 connection.commit()

Currently, I have the following method to execute INSERT/UPDATE/DELETE statements using psycopg2 in Python: def exec_statement(_cxn, _stmt): try: db_crsr = _cxn.cursor() db_crsr.execute(_stmt) _cxn.commit() …
amphibient
  • 29,770
  • 54
  • 146
  • 240
64
votes
9 answers

Checking if a postgresql table exists under python (and probably Psycopg2)

How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.
Hellnar
  • 62,315
  • 79
  • 204
  • 279
62
votes
3 answers

Passing list of parameters to SQL in psycopg2

I have a list of ids of rows to fetch from database. I'm using python and psycopg2, and my problem is how to effectively pass those ids to SQL? I mean that if I know the length of that list, it is pretty easy because I can always manually or…
k_wisniewski
  • 2,439
  • 3
  • 24
  • 31
62
votes
7 answers

psycopg2 insert python dictionary as json

I want to insert a python dictionary as a json into my postgresql database (via python and psycopg2). I have: thedictionary = {'price money': '$1', 'name': 'Google', 'color': '', 'imgurl': 'http://www.google.com/images/nav_logo225.png',…
Rorschach
  • 3,684
  • 7
  • 33
  • 77
62
votes
8 answers

psycopg: Python.h: No such file or directory

I'm compiling psycopg2 and get the following error: Python.h: No such file or directory How to compile it, Ubuntu12 x64.
user2957539
  • 698
  • 1
  • 5
  • 7
62
votes
4 answers

Python psycopg2 not inserting into postgresql table

I'm using the following to try and insert a record into a postgresql database table, but it's not working. I don't get any errors, but there are no records in the table. Do I need a commit or something? I'm using the postgresql database that was…
Superdooperhero
  • 7,584
  • 19
  • 83
  • 138
61
votes
2 answers

Setting application_name on Postgres/SQLAlchemy

Looking at the output of select * from pg_stat_activity;, I see a column called application_name, described here. I see psql sets this value correctly (to psql...), but my application code (psycopg2/SQLAlchemy) leaves it blank. I'd like to set this…
Yaniv Aknin
  • 4,103
  • 3
  • 23
  • 29