Skip to content

Instantly share code, notes, and snippets.

@raulanatol
Created February 12, 2019 13:12
Show Gist options
  • Save raulanatol/513a098129783be312e47ac8de334d68 to your computer and use it in GitHub Desktop.
Save raulanatol/513a098129783be312e47ac8de334d68 to your computer and use it in GitHub Desktop.
Check database connection
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
con = psycopg2.connect(host="host",database="database", user="user", password="password")
cursor = con.cursor()
try:
cursor.execute("""SELECT table_name FROM information_schema.tables""")
for table in cursor.fetchall():
print(table)
except psycopg2.DatabaseError as e:
print('Error %s' % str(e))
sys.exit(1)
finally:
if con:
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment