From bedaf3014380c8b2285f4f6883ce7fb243eee651 Mon Sep 17 00:00:00 2001 From: Rob Speer Date: Fri, 3 Nov 2017 14:35:40 -0400 Subject: [PATCH 1/2] Use a local socket connection to the ConceptNet database --- conceptnet5/db/config.py | 6 +++++- conceptnet5/db/connection.py | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/conceptnet5/db/config.py b/conceptnet5/db/config.py index 4483d4df..daa0470a 100644 --- a/conceptnet5/db/config.py +++ b/conceptnet5/db/config.py @@ -11,7 +11,11 @@ import os DB_USERNAME = os.environ.get('CONCEPTNET_DB_USER', os.environ.get('USER', 'postgres')) +DB_NAME = os.environ.get('CONCEPTNET_DB_NAME', 'conceptnet5') +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')) -DB_NAME = os.environ.get('CONCEPTNET_DB_NAME', 'conceptnet5') + diff --git a/conceptnet5/db/connection.py b/conceptnet5/db/connection.py index 715dcd6a..b9745d25 100644 --- a/conceptnet5/db/connection.py +++ b/conceptnet5/db/connection.py @@ -42,13 +42,21 @@ def get_db_connection(dbname=None, building=False): def _get_db_connection_inner(dbname): - conn = pg8000.connect( - user=config.DB_USERNAME, - password=config.DB_PASSWORD, - host=config.DB_HOSTNAME, - port=config.DB_PORT, - database=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 + ) + pg8000.paramstyle = 'named' return conn From 52cd137619eafdd91075f91456b97e3fab545320 Mon Sep 17 00:00:00 2001 From: Rob Speer Date: Wed, 15 Nov 2017 16:41:01 -0500 Subject: [PATCH 2/2] Allow using a password to connect to the DB, even over a socket (This should help with backward compatibility.) --- conceptnet5/db/config.py | 6 +----- conceptnet5/db/connection.py | 27 ++++++++------------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/conceptnet5/db/config.py b/conceptnet5/db/config.py index daa0470a..e7cdf805 100644 --- a/conceptnet5/db/config.py +++ b/conceptnet5/db/config.py @@ -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')) - diff --git a/conceptnet5/db/connection.py b/conceptnet5/db/connection.py index b9745d25..bb492f04 100644 --- a/conceptnet5/db/connection.py +++ b/conceptnet5/db/connection.py @@ -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