Connecting 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 <node ip address> is the ip address of your primary node, 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.

 

By Instaclustr Support
Need Support?
Experiencing difficulties on the website or console?
Already have an account?
Need help with your cluster?
Contact Support
Why sign up?
To experience the ease of creating and managing clusters via the Instaclustr Console
Spin up a cluster in minutes