Connecting postgresql with sqlalchemy

You would need to pip install SQLAlchemy and pip install psycopg2. An example of a SQLAlchemy connection string that uses psycopg2:

from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')

You could also connect to your database using the psycopg2 driver exclusively:

import psycopg2
conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
conn = psycopg2.connect(conn_string)

However, using the psycopg2 driver to connect does not take advantage of SQLAlchemy.


Yes, psycopg2 are basically the Python drivers for PostgreSQL that need to be installed separately.

A list of valid connection strings can be found here, yours is a bit off (you need to the username, the password and hostname as specified in the link below):

http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql