Skip to content

PostgreSQL: installation and setup

This page covers what to decide and set when a production Cloud SQL instance for FeeCheck is created. Each section states the proof of concept's default, the production setting and why. The commands that created the proof of concept's instance, with its own names, are in the Cloud SQL guide (docs/guides/cloud-sql.md); the commands here use placeholders:

Placeholder Stands for
<project> the Google Cloud project
<region> the instance's region
<instance> the Cloud SQL instance
<runtime> the backend's runtime service account, as a database user: its address without .gserviceaccount.com
<migration> the identity that runs the migrations, in the same form

Every step changes a cloud project and costs money. Run it with the owner's approval and the owner's own gcloud sign-in.

Region

PoC default europe-west6, beside the backend and the document converter (7.1.2); ADR-0013 §7 point 1
Production setting the region the installation's regulatory regime approves (4.7), and the same region as the backend

The database holds contract text, so its region is bound by the same rule as every other store of the installation: data stays in an approved location (SEC-Q3, 10.1.3). Within the approved regions, put the instance where the backend runs. Otherwise every query crosses regions. A cheaper region saves only when the backend moves with it.

A Cloud SQL instance cannot move to another region. A change of region is a new instance, filled from an export or a backup of the old one (Restoring). Decide the region before the instance holds data.

Major version and upgrade path

PoC default PostgreSQL 18, Cloud SQL's default for new instances; the local container runs the same major version; ADR-0013 §7 point 5
Production setting the major version Cloud SQL marks as default when the instance is created, and the same major version in the local container and in the PostgreSQL integration test

Cloud SQL lists each major version with its community end of life (database versions). After that date an instance keeps running under extended support, which is charged, and a deprecated version is eventually upgraded automatically. Upgrade before the date, at a time chosen for it:

  1. Prove the new version offline first. Change the image of the local container in compose.yaml to the new major version, with pgvector, and run ./gradlew postgresIntegrationTest. It migrates an empty database and proves the extension. A failure here costs nothing.
  2. Check the instance. Cloud SQL's pre-check reports what would stop the upgrade, such as an extension that the target version does not support:
gcloud sql instances pre-check-major-version-upgrade <instance> \
  --target-database-version=POSTGRES_<n> --project=<project>
  1. Upgrade in place, in a maintenance window: the instance is unavailable while pg_upgrade runs, and the backend's readiness fails until it is back (Reading the dashboard).
gcloud sql instances patch <instance> --database-version=POSTGRES_<n> --project=<project>

Cloud SQL takes an on-demand backup immediately before the upgrade (in-place upgrade). A backup restores only to an instance of the version it was taken on, so the way back from a failed upgrade is that backup restored to an instance on the old version. 4. Check the extension afterwards: select extversion from pg_extension where extname = 'vector';, and ALTER EXTENSION vector UPDATE; as the administrator when the new version ships a newer one.

Edition, tier and sizing

PoC default Enterprise edition, db-f1-micro: shared core, 0.6 GB memory, 25 connections, outside the Cloud SQL SLA; 10 GB SSD with automatic increase (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §3 point 2)
Production setting Enterprise edition, a dedicated-core tier (db-custom-<vCPUs>-<MB>), so the SLA applies; SSD with automatic increase

Shared core is right for one developer and wrong for production: no SLA, little memory, and few connections (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §2 and §6). Size the dedicated tier from measurements, not from a guess:

  • Memory from the working set: the case's tables, the pages, and the case engine's tables once it runs. The dashboard's CPU tile and Cloud SQL's memory metric show the load of the proof of concept as a lower bound.
  • Connections from the connection budget. Cloud SQL derives max_connections from the tier's memory and adjusts it on a resize; read the value on the instance with select setting from pg_settings where name = 'max_connections';.
  • Storage from the pages, and the point-in-time recovery logs where the instance keeps them on its disk (Backups). Automatic increase only grows: a disk is never shrunk.

The edition's high availability — a standby in a second zone that takes over on a failure — doubles compute and storage. FeeCheck has one instance and no replica, with no failover (8.3, consistency; spec 102 puts replicas and high availability out of scope). An installation whose availability target needs it decides it with that target.

Private IP and the organisation policy

PoC default private IP only, in the project's VPC network through private services access; the organisation policy constraints/sql.restrictPublicIp is enforced (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §3 point 7)
Production setting the same

The production project enforces constraints/sql.restrictPublicIp too, so no instance can be given a public address by mistake. The instance takes an address in the VPC through private services access, set up once per network; the backend reaches it through Cloud Run's direct VPC egress for private ranges only. The commands are steps 2 and 5 of the Cloud SQL guide (docs/guides/cloud-sql.md), with the production project's names.

The consequence for operations: no notebook reaches the database directly. A person works through Cloud SQL Studio in the console, with an IAM database user of their own (below), or through a bastion inside the network.

IAM database authentication

PoC default the database flag cloudsql.iam_authentication=on; the backend signs in through the Cloud SQL Java connector as its runtime service account; no database password exists (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §3 point 5)
Production setting the same, and every person who reaches the database signs in as an IAM user too; the built-in postgres user gets no password anyone keeps

The runtime account needs roles/cloudsql.client and roles/cloudsql.instanceUser. Cloud SQL grants them on the project; an IAM condition holds each to the one instance, so the account holds nothing else on the project — the loop of step 3 in the Cloud SQL guide (docs/guides/cloud-sql.md), with the production instance's resource name in the condition.

A person gets an IAM database user and the same two roles, held to the instance, for as long as the work needs them. Then every sign-in is a Google identity in the audit log, and there is no shared password to rotate.

Migration role and runtime role

PoC default one identity both migrates and serves: Flyway migrates through the backend's own connection on every start, with the rights the first migration needs to create the vector extension (ADR-0013 (docs/adr/adr-0013-postgresql-on-cloud-sql.md) §7 point 8, a trade-off for synthetic data)
Production setting an administrator creates the extension once; a migration identity creates and owns the tables and runs the migrations; the runtime account holds SELECT, INSERT, UPDATE and DELETE on the application's tables, only SELECT on Flyway's, and nothing else

The production setting separates the right to change the schema from the right to change rows, so the running service holds only the second.

The backend does not run this way: it migrates on start through its one data source. What it needs is a migration step that runs as <migration> before the service's new revision starts — for example a Cloud Run job from the same image that runs Flyway and exits — and a service that only validates the schema. The steps below are the database's side. They have not been run on an instance yet: prove them on a test instance before production.

  1. The extension, once, as an administrator (an IAM user holding cloudsqlsuperuser):
CREATE EXTENSION IF NOT EXISTS vector;

The first migration then finds the extension and changes nothing. 2. The two IAM users, both without a database role:

gcloud sql users create <migration> --instance=<instance> --project=<project> \
  --type=cloud_iam_service_account
gcloud sql users create <runtime> --instance=<instance> --project=<project> \
  --type=cloud_iam_service_account
  1. The right to create tables. The administrator lets <migration> create tables in the application's schema, public. <migration> then owns every table it creates, so it can change and drop them, and nothing outside them:
GRANT USAGE, CREATE ON SCHEMA public TO "<migration>";

From PostgreSQL 15, public belongs to the database's owner and grants CREATE to nobody else. A member of the owning role may grant on its objects, so the administrator needs no membership in <migration>; transferring the schema itself (ALTER SCHEMA … OWNER TO) would need one, and is not required. 4. The runtime's rights, as <migration>, before the first migration. Cloud SQL Studio signs a person in as themselves, not as a service account, so these statements run through a connection of <migration>'s own, such as the migration step's. The default privileges cover every table and sequence <migration> creates later; the two ALL grants cover those that already exist when the steps are run again:

GRANT USAGE ON SCHEMA public TO "<runtime>";
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO "<runtime>";
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO "<runtime>";
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO "<runtime>";
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT USAGE, SELECT ON SEQUENCES TO "<runtime>";
  1. Flyway's own table, as <migration>, after the first migration. Flyway creates flyway_schema_history in public, so the default privileges give <runtime> the right to write it too. The service only validates the schema, which reads the table; take the rest away, and again after every run of step 4:
REVOKE INSERT, UPDATE, DELETE ON flyway_schema_history FROM "<runtime>";

A later table that the service must not write gets the same revoke in the migration step. 6. Check as <runtime>: a CREATE TABLE and a write to flyway_schema_history are refused, and the backend's readiness passes.

Database flags

PoC default cloudsql.iam_authentication=on; every other flag at Cloud SQL's default
Production setting cloudsql.iam_authentication=on, plus the logging flags of NFR-4 on the database, set explicitly so that a later change is a visible diff

gcloud sql instances patch --database-flags replaces the whole list: always pass every flag the instance needs, never only the new one. The list is separated by commas, so a value that holds one, such as pgaudit.log=ddl,role, needs another delimiter, named first (gcloud topic escaping): --database-flags=^:^cloudsql.iam_authentication=on:…:pgaudit.log=ddl,role.

Creating the instance

With the decisions above, the instance is created as in step 2 of the Cloud SQL guide (docs/guides/cloud-sql.md), with these differences:

Option PoC Production
--tier db-f1-micro db-custom-<vCPUs>-<MB>
--region europe-west6 <region>
backups --no-backup --backup-start-time=<HH:MM> --enable-point-in-time-recovery --retained-backups-count=<n> --retained-transaction-log-days=<days> (Backups)
--database-flags cloudsql.iam_authentication=on the list of Database flags
database user <runtime>, which also migrates <migration> and <runtime> (above)

Point-in-time recovery is off when an Enterprise edition instance is created with gcloud; only the console turns it on by default (point-in-time recovery). Automated backups are on by default, which is why the proof of concept passes --no-backup. Name both in the create command.

Monitoring resources

PoC default four resources: the email channel to the owner, the uptime check on the backend's readiness, the alert policy FeeCheck backend not ready and the dashboard; created by the commands of the deploy guide's Health and monitoring (docs/guides/feecheck-cloud-run.md), from the files under monitoring/
Production setting the same four, created by the same commands in the production project, with three changes below
  1. The channel goes to a group that is on call, not to one person's address.
  2. The dashboard's connection tile draws its threshold at the tier's max_connections, not at the 25 of db-f1-micro: change the value in monitoring/feecheck-dashboard.json before the create.
  3. The database's ID in the dashboard's Cloud SQL tiles, <project>:<instance>, is the production project's and instance's, and the connection tile's title names the tier's limit.

Each command creates a new resource, so running it twice makes a duplicate. How to read the resources is in the operations manual.