Reconnecting to PostgreSQL to Listen for Notifications After a Restart

What will you learn?

In this guide, you will learn how to seamlessly reconnect and continue listening for notifications from a PostgreSQL database after it has been restarted. This is crucial for ensuring real-time data updates in applications.

Introduction to Problem and Solution

When working with real-time applications that rely on receiving updates from a database, maintaining a continuous connection is essential. However, disruptions like server restarts can lead to dropped listen connections in PostgreSQL. To address this issue, we need a robust strategy to automatically detect disconnections and re-establish listen connections without manual intervention.

Our solution involves implementing a Python script that not only listens for notifications but also monitors the connection status. If a disconnection is detected, such as during a server restart, the script will attempt to reconnect and resume listening for notifications. This approach ensures that your application stays responsive and up-to-date with the latest database changes.


import psycopg2
from psycopg2.extras import wait_select
import select
import time

def listen_notifications():
    conn = None
        conn = psycopg2.connect(dsn="dbname=yourdb user=youruser password=yourpassword")

        cur = conn.cursor()
        cur.execute("LISTEN my_channel;")

        print("Listening on channel 'my_channel'")

        while True:
            if[conn],[],[],5) == ([],[],[]):
                print("Timeout: Checking connection health...")
                    cur.execute("SELECT 1")
                except (Exception, psycopg2.DatabaseError) as error:
                    print(f"Connection lost: {error}. Attempting reconnection.")

                while conn.notifies:
                    notify = conn.notifies.pop(0)
                    print(f"Got NOTIFY: {}, {}, {notify.payload}")

    except Exception as e:
        print(f"Error: {e}")
        if conn is not None:
            print("Connection closed.")

while True:
    print("Attempting to reconnect...")

# Copyright PHD


This code snippet provides a resilient way of handling PostgreSQL notification subscriptions even through database restarts:

  • Establish Connection: Connects to the PostgreSQL database using psycopg2.
  • Listen for Notifications: Executes LISTEN command on the specified channel.
  • Monitor Connection Health: Uses for non-blocking I/O waiting; checks connectivity periodically.
  • Handle Disconnections Gracefully: Closes the current connection upon detection of disconnection and triggers reconnection.
  • Automatic Reconnection: The main loop ensures continuous attempts at reconnection after handling disconnections.

By following this approach, your application can maintain responsiveness to PostgreSQL notifications even during server restarts.

  1. How does this solution handle network interruptions?

  2. The script periodically checks the connection health using simple queries within timeouts set by, enabling it to detect disruptions like network interruptions or service restarts.

  3. Is there any risk of missing notifications during reconnections?

  4. While there might be brief periods where notifications are missed during disconnectivity/reconnect phases, these windows are typically short-lived due to rapid detection and retry logic implemented in the script.

  5. Can I modify this script for multiple channels?

  6. Yes, you can extend the “LISTEN my_channel;” command within your cursor execution phase to include multiple channels based on your application requirements.

  7. What happens when there are no incoming notifications?

  8. The script employs non-blocking waits ( with periodic wake-ups every 5 seconds (timeout=5) to efficiently manage idle times without unnecessary CPU load.

  9. Does restarting Postgres server affect all listeners equally?

  10. Yes, restarting the Postgres server terminates all active LISTEN connections/commands, requiring clients like our script to initiate fresh LISTEN commands post-restart events accordingly.


Implementing auto-reconnect functionality enhances reliability significantly in scenarios requiring high uptime and responsiveness towards live data alterations transmitted via DB events or notices. This empowers developers to maintain seamless interactions amidst potentially volatile environments effortlessly.

Leave a Comment