Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@barseghyanartur
Created July 29, 2015 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barseghyanartur/9a85cc46a0f299bb1ad2 to your computer and use it in GitHub Desktop.
Save barseghyanartur/9a85cc46a0f299bb1ad2 to your computer and use it in GitHub Desktop.
The Password File helper script for PostgreSQL to be used in Django
from __future__ import print_function
__all__ = ('read_pgpass',)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def read_pgpass(dbname):
"""
Reads the pgpass. Returns the postgres settings dict for Django.
:param str dbname:
:return dict:
"""
import os
try:
# See http://stackoverflow.com/questions/14742064/python-os-environhome-works-on-idle-but-not-in-a-script
home_folder = os.path.expanduser('~')
pgpass = os.path.join(home_folder, '.pgpass')
pgpass_lines = open(pgpass).read().split()
except IOError:
print(
"""
You don't have a ~/.pgpass file so we're using a sqlite database.
To switch to a PostgreSQL database, create a ~/.pgpass file
containing it's credentials.
See http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html
"""
)
else:
for match in (dbname, '*'):
for line in pgpass_lines:
words = line.strip().split(':')
if words[2] == match:
return {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': dbname,
'USER': words[3],
'PASSWORD': words[4],
'HOST': words[0],
}
print(
"""
Your ~/.pgpass file doesn't have database '{0}' so we're using
a sqlite database for now.
To switch to a PostgreSQL database, add a line to the ~/.pgpass file
containing it's credentials.
See http://www.postgresql.org/docs/9.3/static/libpq-pgpass.html
""".format(dbname)
)
return {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'var', 'mysite.db'),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment