python cursor.execute psql code example
Example: postgresql psycopg2 select single value
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import psycopg2
import sys
con = None
try:
con = psycopg2.connect(database='testdb', user='postgres',
password='s$cret')
cur = con.cursor()
cur.execute('SELECT version()')
version = cur.fetchone()[0]
print(version)
except psycopg2.DatabaseError as e:
print(f'Error {e}')
sys.exit(1)
finally:
if con:
con.close()