Skip to main content

Connect with Python

This example connects to a PostgreSQL® service from Python, making use of the psycopg2 library.


These are the placeholders you will need to replace in the code sample:

POSTGRESQL_URIURL for PostgreSQL connection, from the service overview page


For this example you will need:

  • Python 3.6 or later

  • The Python psycopg2 library. You can install this with pip:

    pip install psycopg2


Add the following to and replace the placeholders with values for your project:

import psycopg2

def main():
conn = psycopg2.connect('POSTGRESQL_URI')

query_sql = 'SELECT VERSION()'

cur = conn.cursor()

version = cur.fetchone()[0]

if __name__ == "__main__":

This code creates a PostgreSQL client and connects to the database. Then runs a query checking the database version and prints the response


By default, the connection string specifies sslmode=require which does not verify the CA certificate. A better approach for production would be to change it to sslmode=verify-ca and include the certificate.

To run the code:


If the script runs successfully, the outputs should be the PostgreSQL version running in your service like:

PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by gcc, a 68c5366192 p 6520304dc1, 64-bit