Skip to content

Instantly share code, notes, and snippets.

@graphaelli

graphaelli/dsn2pgpass.py

Last active Feb 19, 2016
Embed
What would you like to do?
#!/usr/bin/env python
import fileinput
import urllib.parse
urllib.parse.uses_netloc.append("postgres")
dsn = next(fileinput.input()).strip()
url = urllib.parse.urlparse(dsn)
pgpass = "{hostname}:{port}:{database}:{username}:{password}".format(
database=url.path[1:],
username=url.username,
password=url.password,
hostname=url.hostname,
port=url.port
)
pgpass_dsn = "postgres://{username}@{hostname}:{port}/{database}".format(
database=url.path[1:],
username=url.username,
hostname=url.hostname,
port=url.port
)
print("""cat >> ~/.pgpass << EOF
{}
EOF""".format(pgpass)
)
print("%sql {}".format(pgpass_dsn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment