Completed
Pull Request — develop (#32)
by Antony
02:09
created

build.models.connect()   B

Complexity

Conditions 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
nop 0
1
import psycopg2
2
from datetime import datetime
3
from config import dbconfig, basedir, filename , section, DATABASE_URL
4
import os
5
import jwt
6
7
from flask import json, jsonify
8
from itsdangerous import (TimedJSONWebSignatureSerializer 
9
                            as Serializer, BadSignature, SignatureExpired)
10
11
12
13
14
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
46
47
48
if __name__ == '__main__':
49
    connect()
50
51
    
52