Connect to PostgreSQL with Python

In this article, we will introduce how to connect to a PostgreSQL cluster using Python and some of the possible actions that you can perform on a PostgreSQL database. 

The easiest way to set up a Python client for PostgreSQL is using the Psycopg adapter for Python.

Install psycog

Connecting to a cluster

where <comma separated node ip addresses> is the ip addresses of your cluster nodes, and <password> is the password for the icpostgresql user. You can find this information on the Connection Info page of your cluster.

This will connect you to the postgres database on your cluster, which is created when your cluster is initially provisioned. You can then process SQL statements by opening a cursor.

Creating a table

Populate a table with rows

Querying a table

Make the changes to the database persistent

Close communication with the database

Further Reading

The examples above only give basic use cases for the Psycopg2 adapter.  For more information please see the official psycopg2 documentation.