How do i get PostgreSQL notifications work with python?

1.7k Views Asked by At

i was trying to follow this example from stackoverflow using pg_notify with python. I am not getting this to work. Nothing happens and python does not receive the notification.

Python 3.85 Using Psycopg2. PostgreSQL 13

python & postgresql: reliably check for updates in a specific table

First. I created a python function that listened to the postgreSQL. Then i went to pgadmin and executed

select pg_notify('process', 'update'); 

My python function is below

def dblisten(connection):
    cur = connection.cursor()
    print("inside")
    cur.execute("LISTEN process")
    while True:
        print("listening")
        select.select([connection],[],[])
        connection.poll()
        events = []
        while connection.notifies:
            notify = connection.notifies.pop().payload
            print ("Got NOTIFY:", datetime.datetime.now(), notify.pid, notify.channel, notify.payload)
if __name__ == '__main__':  # If it's executed like a script (not imported)
connection = psycopg2.connect(host='host', user='user',
                            password='password')
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        dblisten(connection)
0

There are 0 best solutions below