Skip to content

Instantly share code, notes, and snippets.

@grisaitis
Created October 3, 2018 19:19
Show Gist options
  • Save grisaitis/172068ac532e13b46d8289e4838d04a2 to your computer and use it in GitHub Desktop.
Save grisaitis/172068ac532e13b46d8289e4838d04a2 to your computer and use it in GitHub Desktop.
Create postgres connection in python using `PG*` environment variables
import os
assert os.environ['PGUSER'] != '', os.environ
connection_string = \
'postgresql://{user}:{password}@{host}/{database}'.format(
user=os.environ['PGUSER'],
password=os.environ['PGPASSWORD'],
host=os.environ['PGHOST'],
database=os.environ['PGDATABASE'])
from sqlalchemy import create_engine
engine = create_engine(connection_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment