| Conditions | 3 |
| Total Lines | 30 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import psycopg2 |
||
| 15 | def connect(): |
||
| 16 | """Test connection to the postgresql server""" |
||
| 17 | |||
| 18 | conn = None |
||
| 19 | try: |
||
| 20 | #read conection parameters |
||
| 21 | params = dbconfig(filename,section) |
||
| 22 | |||
| 23 | #connect to server |
||
| 24 | print('Conecting to the PostgreSQL database...') |
||
| 25 | conn = psycopg2.connect(**params) |
||
| 26 | #create a cursor |
||
| 27 | cur = conn.cursor() |
||
| 28 | |||
| 29 | #execute a statement |
||
| 30 | print('Postgres database version: ') |
||
| 31 | cur.execute('SELECT version()') |
||
| 32 | |||
| 33 | #display the postgress db server version |
||
| 34 | db_version = cur.fetchone() |
||
| 35 | print(db_version) |
||
| 36 | |||
| 37 | #close comm with pgSQL |
||
| 38 | cur.close() |
||
| 39 | except (Exception, psycopg2.DatabaseError) as error: |
||
| 40 | print(error) |
||
| 41 | finally: |
||
| 42 | if conn is not None: |
||
| 43 | conn.close() |
||
| 44 | print('Database connection closed.') |
||
| 45 | |||
| 52 |