Created
February 12, 2019 13:12
-
-
Save raulanatol/513a098129783be312e47ac8de334d68 to your computer and use it in GitHub Desktop.
Check database connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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