Add a database
sg can install and run PostgreSQL or ClickHouse for you. You describe a service inside a VM group, give it a volume for its data, and name the environment variable that holds the admin password. sg does the install, the config, the first-boot setup and the health check.
The rules, up front
- A service lives in a group with
type: vmandcount: 1. One service per group. - The group's image must be
ubuntu:22.04orubuntu:24.04.sginstalls the database from the vendor's own package repository for that release. versionis required and neverlatest.- The data goes on a volume. With exactly one block volume attached,
sguses it; otherwise name it withdata_volume. - The admin password comes from an environment variable named by
admin_password_env.plananddeployrefuse if it's unset or empty, and the error names the variable, never the value.
PostgreSQL
api_version: genesis/v1
application:
name: shop
cluster: lab
groups:
- name: db
count: 1
type: vm
image: ubuntu:24.04
target: {db-0: node-c}
size: {cpu: 2, memory: 4GiB, root_disk: 20GiB}
volumes:
- name: pgdata
pool: default
size: 20GiB
content_type: block
attach: [db-0]
mount: /srv/pgdata
expose:
- port: 5432/tcp
services:
- kind: postgres
version: "17"
admin_password_env: SG_PG_ADMIN_PASSWORD
tuning:
max_connections: 200
shared_buffers: 1GiB
What each part does:
version: "17"is a major-only selector. At plan timesgreads the PostgreSQL apt repository and freezes the newest17.xrelease that exists for both amd64 and arm64. That exact version is what the plan shows and what gets installed. You can also write an exact package version.- The block volume holds the data directory.
sginitialises it only if it's empty and has noPG_VERSIONyet, so a redeploy never re-initialises your data. expose: 5432/tcppublishes the database onnode-c's tailnet address so you can reach it from your workstation.tuningis optional. PostgreSQL takesmax_connectionsandshared_buffers.- Admin user:
postgres, unless you setadmin_user_envto the name of a variable holding a different role name.
Deploy it:
export SG_PG_ADMIN_PASSWORD='choose-something-long'
sg application init --config application.yaml
sg application plan --config application.yaml
sg application deploy --config application.yaml
The plan has a services.postgres row graded high, naming the frozen version, followed by a restart so cloud-init can run the install on first boot. Connect from your workstation:
PGPASSWORD="$SG_PG_ADMIN_PASSWORD" psql -h 100.64.0.13 -U postgres -c 'select version();'
TCP logins use scram-sha-256. The pg_hba.conf is managed by sg; don't edit it by hand.
ClickHouse
api_version: genesis/v1
application:
name: analytics
cluster: lab
groups:
- name: ch
count: 1
type: vm
image: ubuntu:24.04
target: {ch-0: node-b}
size: {cpu: 4, memory: 8GiB, root_disk: 20GiB}
volumes:
- name: chdata
pool: default
size: 50GiB
content_type: block
attach: [ch-0]
mount: /srv/clickhouse
expose:
- port: 9000/tcp
services:
- kind: clickhouse
version: "24.8.4.13"
admin_password_env: SG_CH_ADMIN_PASSWORD
tuning:
max_memory_usage: 4GiB
ClickHouse needs an exact numeric release like 24.8.4.13, one that its apt repository actually offers. A major-only selector like "24" passes init but plan refuses it. The default port is 9000 (the native protocol) and the default admin user is default.
Connecting your app to the database
This is the least finished part of slo-gen, so here's exactly where it stands.
- name: web
# …
refs: {DATABASE_HOST: db-0}
refs declares that each web instance talks to db-0. sg checks that db-0 exists, and when it points at a service group, deploy adds an app.guard.peers change that opens that one path (web instances to the service port) in the cluster's guard and checks it.
What 0.1.0-dev does not do yet:
- set
DATABASE_HOSTinside the instance. Give your app the database address yourself, throughenvor its own config in the image; - decide the general policy for instance-to-instance traffic. Structural checks don't prove denied traffic is actually blocked.
Treat east-west traffic as work in progress and test the path you depend on.
Data safety
Once a volume has held service data, sg records a permanent marker for it in the ledger. The marker survives removing the service from the spec and even destroy. Related refusals you might meet:
| Refusal | Means |
|---|---|
sg:service-data-on-root | The service's data looks like it lives on the root disk. --confirm-root-data <instance> allows only a restart that keeps the root disk |
sg:service-version-unavailable | No release matching version is in the vendor repository for both architectures |
sg:service-version-incompatible | The version change isn't safe for the data already on the volume |
Upgrading the database release is a service upgrade.