Yago Riveiro
Yago Riveiro

Reputation: 727

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?

Upvotes: 7

Views: 8839

Answers (2)

Don Question
Don Question

Reputation: 11614

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.

Upvotes: 11

Skylar Saveland
Skylar Saveland

Reputation: 11464

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

Upvotes: 15

Related Questions