Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ need to download this repository; just use `nix run`:
nix run github:supabase/nix-postgres#start-server 15
```

This will start PostgreSQL 15 on port `5432` on localhost with a temporary directory created by `mktemp -d`. Connect to it:
This will start PostgreSQL 15 on port `5435` on localhost with a temporary directory created by `mktemp -d`. Connect to it:

```
austin@GANON:~$ nix run github:supabase/nix-postgres#start-client 14
Expand Down
2 changes: 1 addition & 1 deletion docker/init.sh → docker/init.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

sudo -u postgres /bin/initdb --locale=C -D /data
sudo -u postgres ln -s /etc/postgresql.conf /data/postgresql.conf
sudo -u postgres /bin/postgres -p 5432 -D /data
sudo -u postgres /bin/postgres -p @PGSQL_DEFAULT_PORT@ -D /data
4 changes: 2 additions & 2 deletions docs/start-client-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ repository:
nix run .#start-server 14
```

Replace the `14` with a `15`, and you'll be using a different version.
Replace the `14` with a `15`, and you'll be using a different version. Optionally you can specify a second argument for the port.

This always uses port 5432.
You likely have a running postgres, so to not cause a conflict, this uses port 5435 by default.

Actually, you don't even need the repository. You can do this from arbitrary
directories, if the left-hand side of the hash character (`.` in this case) is a
Expand Down
11 changes: 9 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
system.aarch64-linux
]; in flake-utils.lib.eachSystem ourSystems (system:
let
pgsqlDefaultPort = "5435";

# The 'pkgs' variable holds all the upstream packages in nixpkgs, which
# we can use to build our own images; it is the common name to refer to
# a copy of nixpkgs which contains all its packages.
Expand Down Expand Up @@ -177,13 +179,16 @@
let
initScript = pkgs.runCommand "docker-init.sh" {} ''
mkdir -p $out/bin
cp ${./docker/init.sh} $out/bin/init.sh
substitute ${./docker/init.sh.in} $out/bin/init.sh \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}'

chmod +x $out/bin/init.sh
'';

postgresqlConfig = pkgs.runCommand "postgresql.conf" {} ''
mkdir -p $out/etc/
substitute ${./tests/postgresql.conf.in} $out/etc/postgresql.conf \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${./tests/util/pgsodium_getkey.sh}"
'';

Expand Down Expand Up @@ -211,7 +216,7 @@

config = {
Cmd = [ "/bin/init.sh" ];
ExposedPorts = { "5432/tcp" = {}; };
ExposedPorts = { "${pgsqlDefaultPort}/tcp" = {}; };
WorkingDir = "/data";
Volumes = { "/data" = { }; };
};
Expand Down Expand Up @@ -249,6 +254,7 @@
start-server = pkgs.runCommand "start-postgres-server" {} ''
mkdir -p $out/bin
substitute ${./tools/run-server.sh.in} $out/bin/start-postgres-server \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
chmod +x $out/bin/start-postgres-server
Expand All @@ -258,6 +264,7 @@
start-client = pkgs.runCommand "start-postgres-client" {} ''
mkdir -p $out/bin
substitute ${./tools/run-client.sh.in} $out/bin/start-postgres-client \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
chmod +x $out/bin/start-postgres-client
Expand Down
2 changes: 1 addition & 1 deletion tests/postgresql.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# - Connection Settings -

listen_addresses = '*' # what IP address(es) to listen on;
#port = 5432 # (change requires restart)
#port = @PGSQL_DEFAULT_PORT@ # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/tmp' # comma-separated list of directories
Expand Down
4 changes: 2 additions & 2 deletions tools/migrate-tool.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ echo "Old server build: PSQL $1"
echo "New server build: PSQL $2"
echo "Upgrade method: $UPGRADE_METHOD"

PORTNO="${2:-5432}"
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
DATDIR=$(mktemp -d)
NEWDAT=$(mktemp -d)
mkdir -p "$DATDIR" "$NEWDAT"
Expand Down Expand Up @@ -117,6 +117,6 @@ if [ "$UPGRADE_METHOD" == "pg_dumpall" ]; then
cat "$SQLDAT" | $NEWVER/bin/psql -h localhost -d postgres

printf "\n\n\n\n"
echo "NOTE: Done, check logs. Stopping the server; new database is located at $NEWDAT"
echo "NOTE: Done, check logs. Stopping the server; new database is located at $NEWDAT"
$NEWVER/bin/pg_ctl stop -D "$NEWDAT"
fi
4 changes: 3 additions & 1 deletion tools/run-client.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ fi

export PATH=$BINDIR/bin:$PATH

exec psql -h localhost postgres
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"

exec psql -p "$PORTNO" -h localhost postgres
4 changes: 2 additions & 2 deletions tools/run-server.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ fi

export PATH=$BINDIR/bin:$PATH

PORTNO="${2:-5432}"
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
DATDIR=$(mktemp -d)
mkdir -p "$DATDIR"

echo "NOTE: using port $PORTNO for server"
echo "NOTE: using temporary directory $DATDIR for data, which will not be removed"
echo "NOTE: you are free to re-use this data directory at will"
echo
echo

initdb -D "$DATDIR" --locale=C
exec postgres -p "$PORTNO" -D "$DATDIR" -k /tmp