Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 14ac54b

Browse files
authored
fix: start-server conflicting with running pg (#21)
When having a running postgresql, the following error is produced: ``` $ nix run .#start-server 15 ... 2023-08-19 15:25:26.476 -05 [1021103] LOG: starting PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.3.0, 64-bit 2023-08-19 15:25:26.476 -05 [1021103] LOG: could not bind IPv6 address "::1": Address already in use 2023-08-19 15:25:26.476 -05 [1021103] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 2023-08-19 15:25:26.476 -05 [1021103] LOG: could not bind IPv4 address "127.0.0.1": Address already in use 2023-08-19 15:25:26.476 -05 [1021103] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 2023-08-19 15:25:26.476 -05 [1021103] WARNING: could not create listen socket for "localhost" 2023-08-19 15:25:26.476 -05 [1021103] FATAL: could not create any TCP/IP sockets 2023-08-19 15:25:26.479 -05 [1021103] LOG: database system is shut down ``` This is because it runs using the default 5432. This changes the default port to 5435 to avoid conflicts. Additionally: - Parametrizes the pg default port for all configs(docker and local) - Adds a port parameter to #start-client, e.g. `nix run .#start-client 15 5433`
1 parent 2555e2e commit 14ac54b

File tree

8 files changed

+21
-12
lines changed

8 files changed

+21
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ need to download this repository; just use `nix run`:
9999
nix run github:supabase/nix-postgres#start-server 15
100100
```
101101

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

104104
```
105105
austin@GANON:~$ nix run github:supabase/nix-postgres#start-client 14

docker/init.sh renamed to docker/init.sh.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
sudo -u postgres /bin/initdb --locale=C -D /data
55
sudo -u postgres ln -s /etc/postgresql.conf /data/postgresql.conf
6-
sudo -u postgres /bin/postgres -p 5432 -D /data
6+
sudo -u postgres /bin/postgres -p @PGSQL_DEFAULT_PORT@ -D /data

docs/start-client-server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ repository:
55
nix run .#start-server 14
66
```
77

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

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

1212
Actually, you don't even need the repository. You can do this from arbitrary
1313
directories, if the left-hand side of the hash character (`.` in this case) is a

flake.nix

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
system.aarch64-linux
2525
]; in flake-utils.lib.eachSystem ourSystems (system:
2626
let
27+
pgsqlDefaultPort = "5435";
28+
2729
# The 'pkgs' variable holds all the upstream packages in nixpkgs, which
2830
# we can use to build our own images; it is the common name to refer to
2931
# a copy of nixpkgs which contains all its packages.
@@ -177,13 +179,16 @@
177179
let
178180
initScript = pkgs.runCommand "docker-init.sh" {} ''
179181
mkdir -p $out/bin
180-
cp ${./docker/init.sh} $out/bin/init.sh
182+
substitute ${./docker/init.sh.in} $out/bin/init.sh \
183+
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}'
184+
181185
chmod +x $out/bin/init.sh
182186
'';
183187

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

@@ -211,7 +216,7 @@
211216

212217
config = {
213218
Cmd = [ "/bin/init.sh" ];
214-
ExposedPorts = { "5432/tcp" = {}; };
219+
ExposedPorts = { "${pgsqlDefaultPort}/tcp" = {}; };
215220
WorkingDir = "/data";
216221
Volumes = { "/data" = { }; };
217222
};
@@ -249,6 +254,7 @@
249254
start-server = pkgs.runCommand "start-postgres-server" {} ''
250255
mkdir -p $out/bin
251256
substitute ${./tools/run-server.sh.in} $out/bin/start-postgres-server \
257+
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
252258
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
253259
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
254260
chmod +x $out/bin/start-postgres-server
@@ -258,6 +264,7 @@
258264
start-client = pkgs.runCommand "start-postgres-client" {} ''
259265
mkdir -p $out/bin
260266
substitute ${./tools/run-client.sh.in} $out/bin/start-postgres-client \
267+
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
261268
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
262269
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
263270
chmod +x $out/bin/start-postgres-client

tests/postgresql.conf.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# - Connection Settings -
5959

6060
listen_addresses = '*' # what IP address(es) to listen on;
61-
#port = 5432 # (change requires restart)
61+
#port = @PGSQL_DEFAULT_PORT@ # (change requires restart)
6262
max_connections = 100 # (change requires restart)
6363
#superuser_reserved_connections = 3 # (change requires restart)
6464
unix_socket_directories = '/tmp' # comma-separated list of directories

tools/migrate-tool.sh.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ echo "Old server build: PSQL $1"
4949
echo "New server build: PSQL $2"
5050
echo "Upgrade method: $UPGRADE_METHOD"
5151

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

119119
printf "\n\n\n\n"
120-
echo "NOTE: Done, check logs. Stopping the server; new database is located at $NEWDAT"
120+
echo "NOTE: Done, check logs. Stopping the server; new database is located at $NEWDAT"
121121
$NEWVER/bin/pg_ctl stop -D "$NEWDAT"
122122
fi

tools/run-client.sh.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ fi
1919

2020
export PATH=$BINDIR/bin:$PATH
2121

22-
exec psql -h localhost postgres
22+
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
23+
24+
exec psql -p "$PORTNO" -h localhost postgres

tools/run-server.sh.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fi
1919

2020
export PATH=$BINDIR/bin:$PATH
2121

22-
PORTNO="${2:-5432}"
22+
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
2323
DATDIR=$(mktemp -d)
2424
mkdir -p "$DATDIR"
2525

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

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

0 commit comments

Comments
 (0)