This repository contains experimental code to package PostgreSQL using nix.
Follow installation guide at installation.
Supported major versions of Postgres are defined in flake.nix basePackages.
Each major version can be built independently.
nix build .#psql_14/bin -o result-14
nix build .#psql_15/bin -o result-15
Builds Postgres 14 & 15 with all of our extensions and symlinks the resulting build artifacts e.g. psql
, pg_dump
etc at result-14
and result-15
respectively.
On first run these commands will take a long time to complete. Intermediate results are cached and reused leading to significantly reduced built times on subsequent runs.
The Postgres build itself is defined upstream in nixpkgs#postgresqlXX. That distribution is frequently updated and historically has released minor and major updates within 10 days. We can optionally override the version number or vendor the derivation to support additional versions.
Note that nixpkgs supports 1 minor version (the most recent) for each major version.
To launch a psql repl with the built version, first start the server:
nix run .#start-server 15
Next, launch the psql client.
nix run .#start-client 15
Both commands are defined in flake.nix
Extensions included with the Postgres distribution come from two sources:
Each extension has a default version that can be easily updated or overriden. Multiple versions can be managed on git branches or concurrently with unique names e.g. supautils-1.1
, supautils-1.2
.
Extension pgTap tests are defined in ./tests/smoke
and can be run via:
nix flake check
Tests execute against Postgres 14 and 15.
The nix command migration-test
supports testing across arbitrary versions (major and minor) using pg_upgrade
and pg_dumpall
It takes the form:
nix run .#migration-test <old_version> <new_version> <upgrade_method>
For example, to test pg_upgrade
as the method for upgrading between the current version of pg14 and current version of pg15, you can run:
nix run .#migration-test 14 15 pg_upgrade
The files that define the test database to be migrated are:
Since nix allows refering to derivations defined on github, we can test upgrades between any arbitrary prior commits
For example, to upgrade from the postgres 14 version defined in commit 388659fcc3c857f2c45eeb397f67b5b7bf9a1b84 to the current version of postgres 14, run:
nix run github:supabase/nix-postgres#migration-test \
$(nix build github:supabase/nix-postgres/388659fcc3c857f2c45eeb397f67b5b7bf9a1b84#psql_14/bin
14
The data.sql
file is currently very minimal. We would extend that to exercise large parts of a complex schema to ensure tests have significant coverage.
GitHub Actions produces:
(Not yet implemented)
The working plan for AMIs is to use nix copy
to move CI build artifacts from s3 to the AMI. There is a detailed write-up in issue 17
Docker images can be produced from the binary assets generated with nix build
Locally, you can produce a docker image using:
nix build .#psql_15/docker -o result-15
docker load -i ./result-15
Which creates an image named postgresql-15:latest
tagged in local image
registry.
You can run that image with:
docker run --rm \
--name supa_nix_local \
-p 5441:5432 \
-d \
-e POSTGRES_DB=postgres \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=postgres \
-d postgresql-15:latest
and connect through psql
docker exec -it supa_nix_local psql -U postgres
Once available upstream, new versions can be added here
Not in prototype
Builtin and runs in CI
nix build .#psql_14/bin -o result-14
Builtin and runs in CI
nix build .#psql_15/bin -o result-15
Support pending upstream merge of PR or can be vendored.
These occur upstream. If we want to stay on the outdated version we can add an override to this repo.
Builtin and runs in CI
- Lack of darwin-arm support: This is blocked by pgrx extensions. Everything except those extensions is functional.
- Difficulty producing docker images in Github Actions CI: solvable with changes to our custom runners
There is a nix binary cache located at the following URL:
Binaries are signed with the public key:
nix-cache.supabase.com-1:ZfEc7Qb7SN+qOTJGMtCz54rnVQ1W2ZI2ROCSSD6YQYc=