Skip to main content

Connect with Python

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

Variables

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

VariableDescription
POSTGRESQL_URIURL for PostgreSQL connection, from the service overview page

Prerequisites

For this example you will need:

  • Python 3.6 or later

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

    pip install psycopg2

Code

Add the following to main.py 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()
cur.execute(query_sql)

version = cur.fetchone()[0]
print(version)


if __name__ == "__main__":
main()

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

note

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:

python main.py

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