PostgreSQL
Each PostgreSQL database runs in its own container with a volume and a health check, on its project's private network. It has no address on the internet, and its password never leaves the server unless someone asks to see it.
Create a database
- Open a project and press New service → PostgreSQL database.
- Give it a name, such as
shop-db. Apps reach it atshop-db:5432. - Pick a version (16 by default).
- Under Size and limits, change the memory (default 256 MB) and the disk quota (default 5 GB) if you like.
- Press Create database. It's ready in a few seconds.
The engine is tuned to its memory: shared_buffers is a quarter of it, and max_connections grows with it (20 to 200).
Several apps can share one database. Add a second only when you want separate data; its suggested name is then <project>-db-2.
Link a database to an app
In the app's Settings, tick the database under Databases and storage. The app restarts with these settings:
DATABASE_URL=postgres://…@shop-db:5432/…
PGHOST=shop-db
PGPORT=5432
PGUSER=…
PGPASSWORD=…
PGDATABASE=…
For a second database, give it a prefix such as ANALYTICS_: the app then gets ANALYTICS_DATABASE_URL and so on.
You can also use a database's values inside any setting:
REPORTS_URL=${{shop-db.DATABASE_URL}}
Both are filled in on the server, so the password never appears in the app's settings in the console.
The database's page
- Connect shows the variables an app needs, with the password hidden. Reveal reads it from the server; every reveal is written to the access log.
- Back up takes a copy now. Copies are also taken on the backup schedule (see Backups).
- Pause stops the database; apps using it stop working until Resume.
- Settings: display name, memory, quota and notes.
- More → Download a dump: a
pg_dumpcustom-format file you can load on your computer withpg_restore. - More → Load a dump: replace the data with a
pg_dump -Fcfile up to 2 GB. A backup is taken first, and linked apps restart afterwards. - More → New password: rotates the password and restarts the apps that use it.
- More → Newer PostgreSQL: upgrades to a newer major version by dump and restore. If anything fails, the old version stays.
- More → Move to project and Remove (owners only; a final dump is kept on the server). A database still linked by apps can't be removed or moved.
Working on the data from your computer
The database has no public port. Download a dump and restore it locally:
createdb shop_copy
pg_restore --no-owner -d shop_copy shop-db.dump
To push data in, make a dump with pg_dump -Fc and use Load a dump.
Quota and full databases
Size is checked every 5 minutes against the quota.
- At 80% the alert addresses get an email.
- At 100% the database becomes read-only (apps can read but not write) and another email goes out.
- Raise the quota in Settings or delete data, and it can write again within 5 minutes.
The quota is a soft limit enforced with PostgreSQL's default_transaction_read_only, not a disk limit.
Limits
- One database per container. No outside port: use dumps.
- No point-in-time recovery: backups are scheduled dumps.