Skip to content

Instantly share code, notes, and snippets.

@anuriq
Created May 14, 2019 13:42
Show Gist options
  • Save anuriq/806cc89e8786f5e9ad2f5f3094497e5e to your computer and use it in GitHub Desktop.
Save anuriq/806cc89e8786f5e9ad2f5f3094497e5e to your computer and use it in GitHub Desktop.
from __future__ import print_function
from datetime import datetime
from random import randint
from time import sleep
import psycopg2
def main():
try:
connection = psycopg2.connect(user = "admin",
password = "P@ssw0rd",
host = "89.208.87.38",
port = "5432",
database = "myproddb")
cursor = connection.cursor()
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("Connection opened to", record[0])
cursor.execute(
"INSERT INTO log VALUES ({});".format(randint(1, 10000)))
connection.commit()
cursor.execute("SELECT COUNT(event_id) from log;")
record = cursor.fetchone()
print("Logged a value, overall count: {}".format(record[0]))
except Exception as error:
print ("Error while connecting to PostgreSQL", error)
finally:
if connection:
cursor.close()
connection.close()
print("Connection closed")
if __name__ == '__main__':
try:
while True:
try:
print(datetime.now())
main()
sleep(3)
except Exception as e:
print("Caught error:\n", e)
sleep(1)
except KeyboardInterrupt:
print("exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment