This repository is a simple demonstration of supplying Postgresql built-binaries via a Nix package, but using and running the Postgres server/cluster outside of the control of nix (for example, supplying your own data dir, postgesql.conf, etc).
This approach should work on Linux, macOS, and windows via wsl2
- Install Nix (this installer turns on the options automatically to install a package from a nix flake url)
- Next, run
nix profile install github:samrose/pgex#pgex
. This command will install the latest Postgresql release binaries to your system $PATH. Once this command completes, you can check that the Postgres binaries are being source by nix:... the$ which initb /home/<youruser>/.nix-profile/bin/initdb $ which postgres /home/<youruser>/.nix-profile/bin/postgres
nix profile install
command is roughly equal toapt install
on Debian-based systems, and installs a pre-built package + fixing system paths to that package. However, you are still able to interact with the binaries outside of the "nix" context, just as you are with other package managers. - Now, for convenience sake, you can run
chmod +x init.sh && ./initsh
and this will populate a database here in the project directory under thepostgres
andpostgres_data
dirs. - The
./init.sh
script simply runs theinitdb
command supplied by thepostgresql
package under the hood. This produces the following directories:postgres postgres_data
initdb
writes a default mutablepostgresql.conf
file underpostgres_data
which is completely customizeable by the user of this package. All of the configuration settings should work as expected. - Run the server with this command
$ postgres -D postgres_data #postgres finds the posgresql.conf in the postgres_data dir automatically 2024-02-23 09:32:34.404 EST [1586346] LOG: starting PostgreSQL 15.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 13.2.0, 64-bit 2024-02-23 09:32:34.405 EST [1586346] LOG: listening on IPv4 address "0.0.0.0", port 5432 2024-02-23 09:32:34.405 EST [1586346] LOG: listening on IPv6 address "::", port 5432 2024-02-23 09:32:34.407 EST [1586346] LOG: listening on Unix socket "/home/sam/pgex/postgres/.s.PGSQL.5432" 2024-02-23 09:32:34.411 EST [1586349] LOG: database system was shut down at 2024-02-23 09:32:12 EST 2024-02-23 09:32:34.415 EST [1586346] LOG: database system is ready to accept connections
- Connect via the
psql
client like this:$ psql -h $PWD/postgres -U postgres psql (15.5) Type "help" for help. postgres=#