[03.02.20] Initial Commit and Postgres Database connection
This commit is contained in:
parent
98fbfce396
commit
46d6970873
3
.gitignore
vendored
3
.gitignore
vendored
@ -127,3 +127,6 @@ dmypy.json
|
|||||||
|
|
||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
|
# configuration files
|
||||||
|
*.ini
|
||||||
|
|||||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
6
src/main.py
Normal file
6
src/main.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from utils.databaseConnect import connect
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
connect()
|
||||||
17
src/utils/databaseConfig.py
Normal file
17
src/utils/databaseConfig.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
def config(filename='configuration/database.ini', section='postgresql'):
|
||||||
|
parser = ConfigParser()
|
||||||
|
parser.read(filename)
|
||||||
|
|
||||||
|
db = {}
|
||||||
|
if parser.has_section(section):
|
||||||
|
params = parser.items(section)
|
||||||
|
for param in params:
|
||||||
|
db[param[0]] = param[1]
|
||||||
|
else:
|
||||||
|
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
|
||||||
|
|
||||||
|
return db
|
||||||
35
src/utils/databaseConnect.py
Normal file
35
src/utils/databaseConnect.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
|
from utils.databaseConfig import config
|
||||||
|
|
||||||
|
def connect():
|
||||||
|
""" Connect to the PostgreSQL database server """
|
||||||
|
conn = None
|
||||||
|
try:
|
||||||
|
# read connection parameters
|
||||||
|
params = config()
|
||||||
|
|
||||||
|
# connect to the PostgreSQL server
|
||||||
|
print('Connecting to the PostgreSQL database...')
|
||||||
|
conn = psycopg2.connect(**params)
|
||||||
|
|
||||||
|
# create a cursor
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# execute a statement
|
||||||
|
print('PostgreSQL database version:')
|
||||||
|
cur.execute('SELECT version()')
|
||||||
|
|
||||||
|
# display the PostgreSQL database server version
|
||||||
|
db_version = cur.fetchone()
|
||||||
|
print(db_version)
|
||||||
|
|
||||||
|
# close the communication with the PostgreSQL
|
||||||
|
cur.close()
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
finally:
|
||||||
|
if conn is not None:
|
||||||
|
conn.close()
|
||||||
|
print('Database connection closed.')
|
||||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
Loading…
x
Reference in New Issue
Block a user