Connecting psql client using public IP

This page describes how to connect a psql client, either by running it locally on your client machine or in the Cloud Shell, to your Cloud SQL instance.

Need help? For help troubleshooting the proxy, see Troubleshooting Cloud SQL Proxy connections. Or, see our Cloud SQL Support page.

Before you begin

Before you can start using a client, you must have:

Using a local client

Using a local client to connect to your Cloud SQL instance involves three high-level tasks:

  1. Install the client.
  2. Configure access to your Cloud SQL instance.
  3. Connect to your Cloud SQL instance.

Install the client

To install the client:

Debian/Ubuntu

Install the psql client from the package manager:

sudo apt-get update
sudo apt-get install postgresql-client

CentOS/RHEL

Install the psql client from the package manager:

sudo yum install postgresql

openSUSE

Install the psql client from the package manager:

sudo zypper install postgresql

Other platforms

  1. Download the PostgreSQL Core Distribution for your platform from the PostgreSQL Downloads page.
    The Core Distribution includes the psql client.
  2. Install the PostgreSQL database, following the directions on the download page.

Configure access to your Cloud SQL instance

To configure access to your instance:

  1. From the client machine, use What's my IP to see the IP address of the client machine.
  2. Copy that IP address.
  3. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  4. Click the instance to open its Overview page, and record its IP address.
  5. Select the Connections tab.
  6. Under Authorized networks, click Add network and enter the IP address of the machine where the client is installed.
  7. Click Done. Then click Save at the bottom of the page to save your changes.
  8. Connect to your instance, either with SSL or without SSL.

Connect to your Cloud SQL instance without encryption

To connect to your instance:

  1. Confirm that you have installed the client and configured access to your instance.
  2. Start the psql client:
    psql "sslmode=disable dbname=postgres user=postgres hostaddr=[INSTANCE_IP]"
    
  3. Enter your password.
  4. The psql prompt appears.

Connect to your Cloud SQL instance using SSL

To connect using SSL, you need:

  • A Certificate Authority (CA) certificate in a server-ca.pem file.
  • A client public key certificate in a client-cert.pem file.
  • A client private key in a client-key.pem file.

If you lack a client certificate and a corresponding private key, create a new client certificate.

Before you begin, confirm that you have installed the client and configured access to your instance.

To connect to your instance using SSL:

  1. Start the psql client:
    psql "sslmode=verify-ca sslrootcert=server-ca.pem \
          sslcert=client-cert.pem sslkey=client-key.pem \
          hostaddr=[INSTANCE_IP] \
          user=postgres dbname=[DB_NAME]"
    

    For example:

    psql "sslmode=verify-ca sslrootcert=server-ca.pem \
          sslcert=client-cert.pem sslkey=client-key.pem \
          hostaddr=01.23.45.67 \
          user=postgres dbname=postgres"
    

    An SSL mode of verify-full is not required; verify-ca is enough because the CA is instance-specific.

    You might also want to create a Connection Service File to manage your connection parameters, especially if you are connecting to more than one instance. For more information, see the PostgreSQL documentation.

  2. Enter the password.
  3. You can confirm that the connection is encrypted by looking for the cipher in the connection information:
    SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)
    

Using the client in the Cloud Shell

  1. Go to the Google Cloud Console.

    Go to the Google Cloud Console

  2. Click the Cloud Shell icon towards the right in the tool bar.

    The Cloud Shell takes a few moments to initialize.

  3. At the Cloud Shell prompt, use the built-in client to connect to your Cloud SQL instance:
    gcloud sql connect [INSTANCE_ID] --user=postgres
    
  4. Enter your password.

The gcloud sql command group does not support connecting to a Cloud SQL instance using SSL/TLS.

What's next