How to use with psycopg2?

Anybody who got Neon working over psycopg2?
I have an existing application in Python that is connecting to a postgres database over psycopg2 and this can not be changed.

I’m trying to do a POC if I can make it connect with Neon so I can replace all the existing static database with an autoscaling one on Neon.

The default options and examples I see are not working for me.
Seems like we need some kind of interoperable something between psycopg2 and one of the existing connectors maybe?
Or can Neon be made compatible with psycopg2 “out of the box”?

Thanks!

Here’s a simple script you can try that connects to Neon via psycopg2 and retrieves the current date. Replace the connection string with your own, which you can find in the Connection Details widget on the Neon Dashboard.

import psycopg2
from psycopg2 import OperationalError

def create_connection(url):
    conn = None
    try:
        conn = psycopg2.connect(url)
        print("Connection to PostgreSQL DB successful")
    except OperationalError as e:
        print(f"The error '{e}' occurred")
    return conn

def execute_query(conn, query):
    cursor = conn.cursor()
    try:
        cursor.execute(query)
        result = cursor.fetchone()
        print(f"Query result: {result}")
    except OperationalError as e:
        print(f"The error '{e}' occurred")

if __name__ == "__main__":
    connection_url = "postgresql://daniel:<password>@ep-damp-paper-944505.us-east-2.aws.neon.tech/neondb"
    connection = create_connection(connection_url)

    if connection:
        current_date_query = "SELECT current_date;"
        execute_query(connection, current_date_query)
        connection.close()

We added a new guide for Python with Psycopg: