Querying with Python and psycopg2

You can connect to your database using the standard psycopg2 library.

import psycopg2

try:
    conn = psycopg2.connect(
        dbname="DATABASE_ID",
        user="USERNAME",
        password="PASSWORD",
        host="postgres.phonesql.com",
        port="5432"
    )
    cur = conn.cursor()
    print(cur.execute("SELECT * FROM your_table LIMIT 10"))
    rows = cur.fetchall()
    for row in rows:
        print(row)
    cur.close()
    conn.close()
except psycopg2.OperationalError as e:
    print(f"Could not connect to the database: {e}")