Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Driver python for postgresql

Which is the best driver in python to connect to postgresql?

There are a few possibilities, http://wiki.postgresql.org/wiki/Python but I don't know which is the best choice

Any idea?

like image 978
Yago Riveiro Avatar asked May 05 '12 19:05

Yago Riveiro


2 Answers

psycopg2 is the one everyone uses with CPython. For PyPy though, you'd want to look at the pure Python ones.

like image 173
Skylar Saveland Avatar answered Oct 18 '22 19:10

Skylar Saveland


I would recommend sqlalchemy - it offers great flexibility and has a sophisticated inteface.

Futhermore it's not bound to postgresql alone.

Shameless c&p from the tutorial:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# an Engine, which the Session will use for connection
# resources
some_engine = create_engine('postgresql://scott:tiger@localhost/')

# create a configured "Session" class
Session = sessionmaker(bind=some_engine)

# create a Session
session = Session()

# work with sess
myobject = MyObject('foo', 'bar')
session.add(myobject)
session.commit()

Clarifications due to comments (update):

sqlalchemy itself is not a driver, but a so called Object Relational Mapper. It does provide and include it's own drivers, which in the postgresql-case is libpq, which itself is wrapped in psycopg2.

Because the OP emphasized he wanted the "best driver" to "connect to postgresql" i pointed sqlalchemy out, even if it might be a false answer terminology-wise, but intention-wise i felt it to be the more useful one.

And even if i do not like the "hair-splitting" dance, i still ended up doing it nonetheless, due to the pressure felt coming from the comments to my answer.

I apologize for any irritations caused by my slander.

like image 23
Don Question Avatar answered Oct 18 '22 19:10

Don Question