Skip to main content

How to provision a managed PostgreSQL database on Cloud SQL

Provision a managed PostgreSQL instance on Google Cloud SQL with private IP, the Auth Proxy, and automated backups. Covers users, databases, and recovery.

Difficulty
Intermediate
Duration
45 minutes
Steps
6

Google Cloud SQL is a managed relational database service for PostgreSQL, MySQL, and SQL Server. It handles patching, replication, and backups so you focus on schema and queries. For production, connect over private IP and the Cloud SQL Auth Proxy rather than exposing a public address.

Prerequisites

  • A Google Cloud project and the gcloud CLI.
  • A VPC for private connectivity.

Steps

1. Enable the API

gcloud services enable sqladmin.googleapis.com

2. Create the instance

gcloud sql instances create app-pg \
  --database-version POSTGRES_16 --tier db-custom-2-7680 --region us-central1

The tier sets vCPUs and memory; size for your workload.

3. Configure private IP

Allocate a private services range and create the instance with --network <vpc> --no-assign-ip so it has no public address. Traffic stays inside your VPC.

4. Create a database and user

gcloud sql databases create appdb --instance app-pg
gcloud sql users create appuser --instance app-pg --password <strong-password>

Prefer IAM database authentication over static passwords where possible.

5. Connect via Auth Proxy

The Cloud SQL Auth Proxy creates an encrypted tunnel and handles IAM:

./cloud-sql-proxy <project>:us-central1:app-pg
psql "host=127.0.0.1 dbname=appdb user=appuser"

6. Enable backups

gcloud sql instances patch app-pg --backup-start-time 03:00 --enable-point-in-time-recovery

Point-in-time recovery lets you restore to any moment within the retention window.

Verification

Connect through the proxy and run SELECT version(); to confirm the PostgreSQL version. Confirm the instance has no public IP with gcloud sql instances describe app-pg. Trigger an on-demand backup and confirm it completes.

Next Steps

Add a read replica for reporting, enable high availability for automatic failover, and store credentials in Secret Manager rather than command lines.

Prerequisites

  • Google Cloud project
  • gcloud CLI installed
  • An existing VPC

Steps

  • 1
    Enable the API
  • 2
    Create the instance
  • 3
    Configure private IP
  • 4
    Create a database and user
  • 5
    Connect via Auth Proxy
  • 6
    Enable backups

Category

Database