diff --git a/README.md b/README.md index 2530f6c..952611b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docker/init.sh b/docker/init.sh.in similarity index 70% rename from docker/init.sh rename to docker/init.sh.in index 518a40d..a0e6199 100644 --- a/docker/init.sh +++ b/docker/init.sh.in @@ -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 diff --git a/docs/start-client-server.md b/docs/start-client-server.md index eec03fa..8031a1b 100644 --- a/docs/start-client-server.md +++ b/docs/start-client-server.md @@ -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 diff --git a/flake.nix b/flake.nix index d96d31f..bca870a 100644 --- a/flake.nix +++ b/flake.nix @@ -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. @@ -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}" ''; @@ -211,7 +216,7 @@ config = { Cmd = [ "/bin/init.sh" ]; - ExposedPorts = { "5432/tcp" = {}; }; + ExposedPorts = { "${pgsqlDefaultPort}/tcp" = {}; }; WorkingDir = "/data"; Volumes = { "/data" = { }; }; }; @@ -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 @@ -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 diff --git a/tests/postgresql.conf.in b/tests/postgresql.conf.in index 0ab28e4..1ec7d60 100644 --- a/tests/postgresql.conf.in +++ b/tests/postgresql.conf.in @@ -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 diff --git a/tools/migrate-tool.sh.in b/tools/migrate-tool.sh.in index 79082cb..716cf8c 100644 --- a/tools/migrate-tool.sh.in +++ b/tools/migrate-tool.sh.in @@ -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" @@ -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 diff --git a/tools/run-client.sh.in b/tools/run-client.sh.in index 830e418..882d36f 100644 --- a/tools/run-client.sh.in +++ b/tools/run-client.sh.in @@ -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 diff --git a/tools/run-server.sh.in b/tools/run-server.sh.in index 21abd3b..b047026 100644 --- a/tools/run-server.sh.in +++ b/tools/run-server.sh.in @@ -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