import sqlalchemy def connect(user, password, db, host='localhost', schema='production', port=5432): '''Returns a connection and a metadata object''' # We connect with the help of the PostgreSQL URL # postgresql://federer:grandestslam@localhost:5432/tennis url = 'postgresql://{}:{}@{}:{}/{}' url = url.format(user, password, host, port, db) # The return value of create_engine() is our connection object con = sqlalchemy.create_engine(url, client_encoding='utf8') # We then bind the connection to MetaData() meta = sqlalchemy.MetaData(bind=con, reflect=True, schema=schema) return con, meta