9951 explained code solutions for 126 technologies


amazon-redshiftHow do I connect to an Amazon Redshift cluster using PostgreSQL?


To connect to an Amazon Redshift cluster using PostgreSQL, you need to have the following information:

  1. The hostname of the cluster
  2. The port number of the cluster
  3. The database name
  4. The username and password

Once you have the above information, you can use the following code to connect to the cluster:

import psycopg2

conn = psycopg2.connect(host="hostname",
                        port="portnumber",
                        dbname="dbname",
                        user="username",
                        password="password")

This will establish a connection to the cluster and return a connection object. You can then use the connection object to execute queries and other operations.

Helpful links

  1. Connecting to an Amazon Redshift Cluster Using PostgreSQL
  2. psycopg2 - PostgreSQL database adapter for Python

Edit this code on GitHub