Skip to content

Commit 1e9d0e9

Browse files
authored
Merge pull request #2516 from abrightwell/abrightwell-fix-devcontainer
Fix a few issues in `devcontainers`.
2 parents ae1852b + bb72444 commit 1e9d0e9

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

.devcontainer/docker-compose.yml

Lines changed: 16 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"
@@ -45,14 +47,16 @@ services:
4547
- ../testsetup/pg_ssl_init.sh:/docker-entrypoint-initdb.d/02-ssl-init.sh:ro
4648
- ../testsetup/pg_hba_devcontainer.conf:/etc/postgresql/pg_hba.conf:ro
4749
- ../testsetup/certs:/etc/postgresql/ssl:ro
50+
- ../testsetup/postgresql_ssl.conf:/etc/postgresql/postgresql_ssl.conf:ro
4851
- pg-sockets:/var/run/postgresql
4952
network_mode: service:app
5053
environment:
5154
POSTGRES_USER: postgres
5255
POSTGRES_PASSWORD: postgres
5356
POSTGRES_DB: pgx_test
5457
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
58+
PGPORT: 5414
59+
command: postgres -c port=5414 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
5660

5761
postgres-15:
5862
image: postgres:15
@@ -63,14 +67,16 @@ services:
6367
- ../testsetup/pg_ssl_init.sh:/docker-entrypoint-initdb.d/02-ssl-init.sh:ro
6468
- ../testsetup/pg_hba_devcontainer.conf:/etc/postgresql/pg_hba.conf:ro
6569
- ../testsetup/certs:/etc/postgresql/ssl:ro
70+
- ../testsetup/postgresql_ssl.conf:/etc/postgresql/postgresql_ssl.conf:ro
6671
- pg-sockets:/var/run/postgresql
6772
network_mode: service:app
6873
environment:
6974
POSTGRES_USER: postgres
7075
POSTGRES_PASSWORD: postgres
7176
POSTGRES_DB: pgx_test
7277
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
78+
PGPORT: 5415
79+
command: postgres -c port=5415 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
7480

7581
postgres-16:
7682
image: postgres:16
@@ -81,14 +87,16 @@ services:
8187
- ../testsetup/pg_ssl_init.sh:/docker-entrypoint-initdb.d/02-ssl-init.sh:ro
8288
- ../testsetup/pg_hba_devcontainer.conf:/etc/postgresql/pg_hba.conf:ro
8389
- ../testsetup/certs:/etc/postgresql/ssl:ro
90+
- ../testsetup/postgresql_ssl.conf:/etc/postgresql/postgresql_ssl.conf:ro
8491
- pg-sockets:/var/run/postgresql
8592
network_mode: service:app
8693
environment:
8794
POSTGRES_USER: postgres
8895
POSTGRES_PASSWORD: postgres
8996
POSTGRES_DB: pgx_test
9097
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
98+
PGPORT: 5416
99+
command: postgres -c port=5416 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
92100

93101
postgres-17:
94102
image: postgres:17
@@ -99,14 +107,16 @@ services:
99107
- ../testsetup/pg_ssl_init.sh:/docker-entrypoint-initdb.d/02-ssl-init.sh:ro
100108
- ../testsetup/pg_hba_devcontainer.conf:/etc/postgresql/pg_hba.conf:ro
101109
- ../testsetup/certs:/etc/postgresql/ssl:ro
110+
- ../testsetup/postgresql_ssl.conf:/etc/postgresql/postgresql_ssl.conf:ro
102111
- pg-sockets:/var/run/postgresql
103112
network_mode: service:app
104113
environment:
105114
POSTGRES_USER: postgres
106115
POSTGRES_PASSWORD: postgres
107116
POSTGRES_DB: pgx_test
108117
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
118+
PGPORT: 5417
119+
command: postgres -c port=5417 -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
110120

111121
postgres-18:
112122
image: postgres:18
@@ -117,14 +127,15 @@ services:
117127
- ../testsetup/pg_ssl_init.sh:/docker-entrypoint-initdb.d/02-ssl-init.sh:ro
118128
- ../testsetup/pg_hba_devcontainer.conf:/etc/postgresql/pg_hba.conf:ro
119129
- ../testsetup/certs:/etc/postgresql/ssl:ro
130+
- ../testsetup/postgresql_ssl.conf:/etc/postgresql/postgresql_ssl.conf:ro
120131
- pg-sockets:/var/run/postgresql
121132
network_mode: service:app
122133
environment:
123134
POSTGRES_USER: postgres
124135
POSTGRES_PASSWORD: postgres
125136
POSTGRES_DB: pgx_test
126137
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
138+
command: postgres -c hba_file=/etc/postgresql/pg_hba.conf -c unix_socket_directories=/var/run/postgresql
128139

129140
cockroachdb:
130141
image: cockroachdb/cockroach:v25.4.4

testsetup/pg_ssl_init.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
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 /etc/postgresql/postgresql_ssl.conf >> "$PGDATA/postgresql.conf"

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.

0 commit comments

Comments
 (0)