Skip to content

Commit

Permalink
Allow using a password to connect to the DB, even over a socket
Browse files Browse the repository at this point in the history
(This should help with backward compatibility.)
  • Loading branch information
Rob Speer committed Nov 15, 2017
1 parent bedaf30 commit 52cd137
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
6 changes: 1 addition & 5 deletions conceptnet5/db/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

DB_USERNAME = os.environ.get('CONCEPTNET_DB_USER', os.environ.get('USER', 'postgres'))
DB_NAME = os.environ.get('CONCEPTNET_DB_NAME', 'conceptnet5')
DB_PASSWORD = os.environ.get('CONCEPTNET_DB_PASSWORD')
DB_SOCKET = '/var/run/postgresql/.s.PGSQL.5432'

# These will not be used if DB_PASSWORD is blank -- instead, we'll use DB_SOCKET
DB_PASSWORD = os.environ.get('CONCEPTNET_DB_PASSWORD', '')
DB_HOSTNAME = os.environ.get('CONCEPTNET_DB_HOSTNAME', 'localhost')
DB_PORT = int(os.environ.get('CONCEPTNET_DB_PORT', '5432'))

27 changes: 8 additions & 19 deletions conceptnet5/db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,22 @@ def get_db_connection(dbname=None, building=False):
except pg8000.InterfaceError:
if attempt == 0:
print(
"Database %r at %s:%s is not available, retrying for 10 seconds"
% (dbname, config.DB_HOSTNAME, config.DB_PORT),
"Database %r is not available, retrying for 10 seconds" % dbname,
file=sys.stderr
)
time.sleep(1)
raise IOError(
"Couldn't connect to database %r at %s:%s" %
(dbname, config.DB_HOSTNAME, config.DB_PORT)
"Couldn't connect to database %r" % dbname
)


def _get_db_connection_inner(dbname):
if not config.DB_PASSWORD:
conn = pg8000.connect(
user=config.DB_USERNAME,
unix_sock=config.DB_SOCKET,
database=dbname
)
else:
conn = pg8000.connect(
user=config.DB_USERNAME,
password=config.DB_PASSWORD,
host=config.DB_HOSTNAME,
port=config.DB_PORT,
database=dbname
)

conn = pg8000.connect(
user=config.DB_USERNAME,
password=config.DB_PASSWORD or None,
unix_sock=config.DB_SOCKET,
database=dbname
)
pg8000.paramstyle = 'named'
return conn

Expand Down

0 comments on commit 52cd137

Please sign in to comment.