Skip to content

Commit 6afbb07

Browse files
committed
While working on jackc#2510 I encountered some issues with devcontainers that were introduced by 9b5e030e:
* Unsupported client encoding for CRDB. * SSL command-line flags cause the temp server to fail before initdb has copied the certs. * `:whoami` user creation, when the user already exists. * Postgres instances sharing `/var/run/postgresql` race on the default socket port. These changes should resolve each.
1 parent 6e1e9eb commit 6afbb07

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

.devcontainer/docker-compose.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ services:
2121
PGPASSWORD: postgres
2222
PGDATABASE: pgx_test
2323
PGHOST: localhost
24+
PGCLIENTENCODING: utf8
25+
2426
# PGX test env vars target PG18 (port 5432) by default.
2527
# test.sh overrides these per-target.
2628
PGX_TEST_DATABASE: "host=localhost port=5432 user=postgres password=postgres dbname=pgx_test"
@@ -52,7 +54,8 @@ services:
5254
POSTGRES_PASSWORD: postgres
5355
POSTGRES_DB: pgx_test
5456
POSTGRES_HOSTNAME: localhost
55-
command: postgres -c port=5414 -c hba_file=/etc/postgresql/pg_hba.conf -c ssl=on -c ssl_cert_file=server.crt -c ssl_key_file=server.key -c ssl_ca_file=root.crt -c unix_socket_directories=/var/run/postgresql
57+
PGPORT: 5414
58+
command: postgres -c port=5414 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
5659

5760
postgres-15:
5861
image: postgres:15
@@ -70,7 +73,8 @@ services:
7073
POSTGRES_PASSWORD: postgres
7174
POSTGRES_DB: pgx_test
7275
POSTGRES_HOSTNAME: localhost
73-
command: postgres -c port=5415 -c hba_file=/etc/postgresql/pg_hba.conf -c ssl=on -c ssl_cert_file=server.crt -c ssl_key_file=server.key -c ssl_ca_file=root.crt -c unix_socket_directories=/var/run/postgresql
76+
PGPORT: 5415
77+
command: postgres -c port=5415 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
7478

7579
postgres-16:
7680
image: postgres:16
@@ -88,7 +92,8 @@ services:
8892
POSTGRES_PASSWORD: postgres
8993
POSTGRES_DB: pgx_test
9094
POSTGRES_HOSTNAME: localhost
91-
command: postgres -c port=5416 -c hba_file=/etc/postgresql/pg_hba.conf -c ssl=on -c ssl_cert_file=server.crt -c ssl_key_file=server.key -c ssl_ca_file=root.crt -c unix_socket_directories=/var/run/postgresql
95+
PGPORT: 5416
96+
command: postgres -c port=5416 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
9297

9398
postgres-17:
9499
image: postgres:17
@@ -106,7 +111,8 @@ services:
106111
POSTGRES_PASSWORD: postgres
107112
POSTGRES_DB: pgx_test
108113
POSTGRES_HOSTNAME: localhost
109-
command: postgres -c port=5417 -c hba_file=/etc/postgresql/pg_hba.conf -c ssl=on -c ssl_cert_file=server.crt -c ssl_key_file=server.key -c ssl_ca_file=root.crt -c unix_socket_directories=/var/run/postgresql
114+
PGPORT: 5417
115+
command: postgres -c port=5417 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
110116

111117
postgres-18:
112118
image: postgres:18
@@ -124,7 +130,7 @@ services:
124130
POSTGRES_PASSWORD: postgres
125131
POSTGRES_DB: pgx_test
126132
POSTGRES_HOSTNAME: localhost
127-
command: postgres -c hba_file=/etc/postgresql/pg_hba.conf -c ssl=on -c ssl_cert_file=server.crt -c ssl_key_file=server.key -c ssl_ca_file=root.crt -c unix_socket_directories=/var/run/postgresql
133+
command: postgres -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
128134

129135
cockroachdb:
130136
image: cockroachdb/cockroach:v25.4.4

testsetup/pg_ssl_init.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#!/bin/bash
2-
# Docker initdb script: copies SSL certificates to PGDATA with correct permissions.
3-
# Runs as the postgres user during container initialization.
2+
# Docker initdb script: copies SSL certificates to PGDATA with correct
3+
# permissions and enables SSL. Runs as the postgres user during container
4+
# initialization.
45
base64 -d /etc/postgresql/ssl/localhost.crt.b64 > "$PGDATA/server.crt"
56
base64 -d /etc/postgresql/ssl/localhost.key.b64 > "$PGDATA/server.key"
67
base64 -d /etc/postgresql/ssl/ca.pem.b64 > "$PGDATA/root.crt"
78
chmod 600 "$PGDATA/server.key"
9+
10+
# Append SSL config to postgresql.conf rather than using command-line flags,
11+
# because the docker entrypoint passes command-line args to the temporary server
12+
# it starts before initdb scripts run. That temp server would fail with ssl=on
13+
# since the cert files don't exist yet.
14+
cat >> "$PGDATA/postgresql.conf" <<'EOF'
15+
ssl = on
16+
ssl_cert_file = 'server.crt'
17+
ssl_key_file = 'server.key'
18+
ssl_ca_file = 'root.crt'
19+
EOF

testsetup/postgresql_setup.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ set password_encryption = 'scram-sha-256';
1212
create user pgx_pw with superuser PASSWORD 'secret';
1313
create user pgx_scram with superuser PASSWORD 'secret';
1414
create user pgx_oauth with superuser;
15-
\set whoami `whoami`
16-
create user :whoami with superuser; -- unix domain socket user
1715

16+
-- When running in devcontainers, `whoami` will be `postgres`. Since the
17+
-- `postgres` user already exists, attempting to recreate it will fail.
18+
-- Therefore, we'll guard against that by no-op'ing if/when the user already
19+
-- exists and thereby not aborting the remaining setup.
20+
\set whoami `whoami`
21+
select format('create user %I with superuser', :'whoami')
22+
where not exists (select from pg_roles where rolname = :'whoami') \gexec
1823

1924
-- The tricky test user, below, has to actually exist so that it can be used in a test
2025
-- of aclitem formatting. It turns out aclitems cannot contain non-existing users/roles.

testsetup/postgresql_ssl.conf

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)