With WAL-G backing up a containerised PostgreSQL, how do you point it at an S3 bucket and choose compression and encryption?
answer
- one prefix variable per storage
- env vars or a config file
- lz4 unless told otherwise
- client-side key vs bucket-side SSE
- what archive_command's process can see
basics
~10 sSet WALG_S3_PREFIX (s3://bucket/path) and AWS credentials, choose WALG_COMPRESSION_METHOD (lz4 by default; zstd, lzma, brotli or none) and encrypt client-side with WALG_LIBSODIUM_KEY or a PGP key, all where the PostgreSQL server process can read them.
solid answer
~40 sWAL-G reads every setting from environment variables or a config file (`--config`, `WALG_CONFIG_PATH`, or `.walg` in JSON, YAML or envfile form in the user's home). One prefix picks the storage: `WALG_S3_PREFIX=s3://startup-pg/prod` with `AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY` and `AWS_REGION`, plus `AWS_ENDPOINT` and `AWS_S3_FORCE_PATH_STYLE` for an S3-compatible service; `WALG_GS_PREFIX` and `WALG_AZ_PREFIX` do the same for GCS and Azure. `WALG_COMPRESSION_METHOD` defaults to `lz4` (fastest, weakest ratio); `zstd` and `brotli` compress about three times better, `lzma` better still but slowly. `WALG_LIBSODIUM_KEY` or `WALG_PGP_KEY` encrypts before upload; `WALG_S3_SSE` only asks the bucket to encrypt at rest. In a container the settings must reach the postgres server, because `archive_command` inherits the server's environment, not your `docker exec` shell.
code
json · 8 lines{
"WALG_S3_PREFIX": "s3://startup-pg-backups/prod",
"AWS_REGION": "eu-west-1",
"WALG_COMPRESSION_METHOD": "zstd",
"WALG_LIBSODIUM_KEY_PATH": "/run/secrets/walg_key",
"WALG_LIBSODIUM_KEY_TRANSFORM": "hex",
"PGHOST": "/var/run/postgresql"
}go deeper
Recall the three knobs: the storage prefix such as WALG_S3_PREFIX, WALG_COMPRESSION_METHOD with lz4 as the default, and a client-side key such as WALG_LIBSODIUM_KEY.
Explain the difference between client-side encryption and S3 server-side encryption, and why the setting has to reach the PostgreSQL server's environment rather than your shell.
Show how you would keep keys and credentials out of the image, prove the archive path with pg_switch_wal, and make sure the restore host can get the decryption key on a bad day.
Weigh compression CPU against storage and egress cost, and decide where encryption keys live so a single compromised or deleted account cannot destroy both the data and its backups.
## How WAL-G reads its configuration WAL-G is a single binary that is started fresh for every operation: a scheduled `wal-g backup-push`, and one `wal-g wal-push` for every WAL segment that PostgreSQL hands to `archive_command`. Each start re-reads its settings from two places: - **Environment variables.** Every documented setting (`WALG_*`, `AWS_*`, `GOOGLE_*`, `AZURE_*`) can simply be exported. - **A config file.** Pass it with `--config /path`, name it in `WALG_CONFIG_PATH`, or let WAL-G find `.walg` in the home directory of the user running it. Any format the viper library reads works: JSON, YAML, envfile. The connection to PostgreSQL uses the ordinary libpq variables `PGHOST`, `PGPORT`, `PGUSER` and `PGPASSWORD`/`PGPASSFILE`; `PGHOST=/var/run/postgresql` makes WAL-G use the local Unix socket, which its docs prefer on the same host. ## Choosing the storage One **prefix variable** selects both the backend and the path inside it: | Storage | Required setting | Credentials | |---|---|---| | S3 or S3-compatible | `WALG_S3_PREFIX=s3://bucket/path` | `AWS_ACCESS_KEY_ID` + `AWS_SECRET_ACCESS_KEY`, `~/.aws/credentials` with `AWS_PROFILE`, or instance metadata | | Google Cloud Storage | `WALG_GS_PREFIX=gs://bucket/path` | `GOOGLE_APPLICATION_CREDENTIALS` or the metadata service | | Azure | `WALG_AZ_PREFIX=azure://container/path` + `AZURE_STORAGE_ACCOUNT` | default credential chain, `AZURE_STORAGE_ACCESS_KEY` or `AZURE_STORAGE_SAS_TOKEN` | | Local or NFS path | `WALG_FILE_PREFIX` | none; not a disaster-recovery copy when it sits on the database's own disk | For a self-hosted S3-compatible service add `AWS_ENDPOINT` (for example `http://objects:9000`), `AWS_S3_FORCE_PATH_STYLE=true` and `AWS_REGION`. Under the prefix WAL-G keeps two folders: `basebackups_005/` for base backups and `wal_005/` for archived WAL. ## Compression `WALG_COMPRESSION_METHOD` applies to backups and WAL alike: | Method | Trade-off (WAL-G's own rough figures) | |---|---| | `lz4` (default) | fastest, weakest ratio | | `zstd` | about 3x better than lz4; tune with `WALG_ZSTD_LEVEL` = `fastest`, `default`, `better`, `best` | | `brotli` | about 3x better than lz4; only in a binary built with `USE_BROTLI` | | `lzma` | about 6x better than lz4, much slower | | `none` | no compression | Every uploaded object carries its algorithm in its extension (`.lz4`, `.zst`, `.br`, `.lzma`), and fetching picks the decompressor from that extension. Switching method later therefore only affects new objects; older backups stay readable. Measure on your own data: a mostly-text database compresses very differently from one full of already-compressed bytea. ## Throughput and cost knobs A few more storage-side settings decide how hard WAL-G leans on the network, the disk and the bill: - `WALG_UPLOAD_CONCURRENCY` (default 16) and `WALG_DOWNLOAD_CONCURRENCY` (default 10) set how many parallel streams uploads and downloads use. - `WALG_NETWORK_RATE_LIMIT` and `WALG_DISK_RATE_LIMIT` cap bytes per second during `backup-push`, so a nightly backup does not starve the application's own I/O on a small instance. - `WALG_S3_STORAGE_CLASS` (default `STANDARD`) can move objects to a cheaper class such as `STANDARD_IA`; check retrieval pricing before a restore depends on it. - `WALG_TAR_SIZE_THRESHOLD` (about 1 GB) sizes each tar part: smaller parts mean finer-grained restores but more storage requests. ## Encryption Two different things are called encryption here: - **Client-side** (the bucket only ever holds ciphertext): `WALG_LIBSODIUM_KEY` or `WALG_LIBSODIUM_KEY_PATH` (a 32-byte key; `WALG_LIBSODIUM_KEY_TRANSFORM` = `hex`, `base64` or `none`; needs a binary built with `USE_LIBSODIUM`), `WALG_PGP_KEY` / `WALG_PGP_KEY_PATH` with `WALG_PGP_KEY_PASSPHRASE`, or `WALG_CSE_KMS_ID` for an AWS KMS key. - **Server-side**: `WALG_S3_SSE=AES256` or `aws:kms` (with `WALG_S3_SSE_KMS_ID`) asks S3 to encrypt at rest. Anyone with read access to the bucket still reads plaintext. The same key must be available to `backup-fetch` and `wal-fetch` on the day you restore; a key that only lived inside the dead container is a lost backup. ## The container trap The most common first-week failure is configuration that reaches your shell but not the server: 1. `archive_command` runs as a child of the PostgreSQL server and inherits the server's environment. Variables exported in a `docker exec` session are invisible to it, so a manual `backup-push` succeeds while every `wal-push` fails. 2. Put the settings into the environment the PostgreSQL process starts with, or into a config file the postgres user can read, with `WALG_CONFIG_PATH` set in that same environment. 3. Mount secrets as files (`WALG_LIBSODIUM_KEY_PATH`, `WALG_PGP_KEY_PATH`) instead of baking them into the image. 4. Prove the archive path end to end: run `SELECT pg_switch_wal();` and check that a new object appears under `wal_005/`.
- Why can a manual wal-g backup-push succeed while every wal-push from archive_command fails?Because the two run with different environments. Your `docker exec` shell has the `WALG_*` and `AWS_*` variables you exported; `archive_command` is forked by the PostgreSQL server and sees only the server's environment. Fix it by giving the server process the variables, or a config file plus `WALG_CONFIG_PATH`, and confirm with `pg_switch_wal()` that a segment lands under `wal_005/`.
- Can you switch WALG_COMPRESSION_METHOD from lz4 to zstd on a running setup?Yes. Only objects written afterwards use zstd; each object's extension (`.lz4`, `.zst`) tells `backup-fetch` and `wal-fetch` which decompressor to use, so older backups and WAL stay restorable. Choosing `brotli` instead needs a binary built with `USE_BROTLI`, so check the binary first.
- WAL-G compresses with zstd unless you configure something else
- WALG_S3_SSE means the bucket never sees unencrypted backup data
- Variables exported in a docker exec shell also configure archive_command
- Changing WALG_COMPRESSION_METHOD makes every older backup unreadable
- A WALG_FILE_PREFIX folder on the database volume counts as an off-site backup