Ho to build Connection URL for Google Cloud SQL Postgresql Instance

4/23/2019

I want to connect my app to a managed postgresql instance on google cloud SQL. The app would be deployed via GKE. Normally, i'd connect via a connection string:

Eg: postgres://<user>:<password>@<my-postgres-host>:5432"

But the documentation states that:

  1. Create a Secret to provide the PostgreSQL username and password to the database.

  2. Update your pod configuration file with the following items:

    • Provide the Cloud SQL instance's private IP address as the host address your application will use to access your database.
    • Provide the Secret you previously created to enable the application to log into the database.
  3. Bring up your Deployment using the Kubernetes manifest file.

I can do step 1 and 3 but cannot follow step 2. Should the connection URL just be: postgres://<PRIVATE_ID>:5432 and I add ENV variables POSTGRES_USER and POSTGRES_PASSWORD through a secret?

Are there any examples I can look up?

Outcome: I'd like to derive the connection url for postgresql hosted on google cloud sql.

Thank you in advance!

-- Panda
google-cloud-sql
google-kubernetes-engine
postgresql

1 Answer

4/23/2019

You can find example of postgres_deployment.yaml file, to deploy with kubernetes. Example users proxy, but database configuration section does not change for private IP. For private IP do not use the section [proxy_container]

This is the section stated in the documentation, that you are searching for: database environment variables and secrets section.

          # The following environment variables will contain the database host,
          # user and password to connect to the PostgreSQL instance.
          env:
            - name: POSTGRES_DB_HOST
              value: 127.0.0.1:5432
            # [START cloudsql_secrets]
            - name: POSTGRES_DB_USER
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: username
            - name: POSTGRES_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: password
            # [END cloudsql_secrets]
-- Pawel Czuczwara
Source: StackOverflow