Skip to content

Commit a823ed7

Browse files
committed
feat: change bootstrap user to supabase_admin upon upgrade
1 parent 86687f8 commit a823ed7

File tree

12 files changed

+438
-20
lines changed

12 files changed

+438
-20
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ if [ -n "$IS_CI" ]; then
7373
echo "PGVERSION: $PGVERSION"
7474
fi
7575

76+
OLD_BOOTSTRAP_USER=$(run_sql -A -t -c "select rolname from pg_authid where oid = 10;")
77+
7678
cleanup() {
7779
UPGRADE_STATUS=${1:-"failed"}
7880
EXIT_CODE=${?:-0}
@@ -352,10 +354,15 @@ function initiate_upgrade {
352354

353355
echo "7. Disabling extensions and generating post-upgrade script"
354356
handle_extensions
355-
356-
echo "8. Granting SUPERUSER to postgres user"
357+
358+
echo "8.1. Granting SUPERUSER to postgres user"
357359
run_sql -c "ALTER USER postgres WITH SUPERUSER;"
358360

361+
echo "8.2. Swap postgres & supabase_admin roles if upgrading from a project with postgres as bootstrap user"
362+
if [ "$OLD_BOOTSTRAP_USER" = "postgres" ]; then
363+
swap_postgres_and_supabase_admin
364+
fi
365+
359366
if [ -z "$IS_NIX_UPGRADE" ]; then
360367
if [ -d "/usr/share/postgresql/${PGVERSION}" ]; then
361368
mv "/usr/share/postgresql/${PGVERSION}" "/usr/share/postgresql/${PGVERSION}.bak"
@@ -375,17 +382,26 @@ function initiate_upgrade {
375382
rm -rf "${PGDATANEW:?}/"
376383

377384
if [ "$IS_NIX_UPGRADE" = "true" ]; then
378-
LC_ALL=en_US.UTF-8 LC_CTYPE=$SERVER_LC_CTYPE LC_COLLATE=$SERVER_LC_COLLATE LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LOCALE_ARCHIVE=/usr/lib/locale/locale-archive su -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $PGBINNEW/initdb --encoding=$SERVER_ENCODING --lc-collate=$SERVER_LC_COLLATE --lc-ctype=$SERVER_LC_CTYPE -L $PGSHARENEW -D $PGDATANEW/" -s "$SHELL" postgres
385+
LC_ALL=en_US.UTF-8 LC_CTYPE=$SERVER_LC_CTYPE LC_COLLATE=$SERVER_LC_COLLATE LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LOCALE_ARCHIVE=/usr/lib/locale/locale-archive su -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $PGBINNEW/initdb --encoding=$SERVER_ENCODING --lc-collate=$SERVER_LC_COLLATE --lc-ctype=$SERVER_LC_CTYPE -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
379386
else
380-
su -c "$PGBINNEW/initdb -L $PGSHARENEW -D $PGDATANEW/" -s "$SHELL" postgres
387+
su -c "$PGBINNEW/initdb -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
381388
fi
382389

390+
# This line avoids the need to supply the supabase_admin password on the old
391+
# instance, since pg_upgrade connects to the db as supabase_admin using unix
392+
# sockets, which is gated behind scram-sha-256 per pg_hba.conf.j2. The new
393+
# instance is unaffected.
394+
echo "local all supabase_admin trust
395+
$(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
396+
run_sql -c "select pg_reload_conf();"
397+
383398
UPGRADE_COMMAND=$(cat <<EOF
384399
time ${PGBINNEW}/pg_upgrade \
385400
--old-bindir="${PGBINOLD}" \
386401
--new-bindir=${PGBINNEW} \
387402
--old-datadir=${PGDATAOLD} \
388403
--new-datadir=${PGDATANEW} \
404+
--username=supabase_admin \
389405
--jobs="${WORKERS}" -r \
390406
--old-options='-c config_file=${POSTGRES_CONFIG_PATH}' \
391407
--old-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES}'" \

ansible/playbook.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@
104104
loop: "{{ sql_files }}"
105105
when: debpkg_mode or stage2_nix
106106

107+
- name: Create postgres role
108+
become: yes
109+
become_user: postgres
110+
shell:
111+
cmd: /usr/lib/postgresql/bin/psql --username=supabase_admin -d postgres -c "create role postgres superuser login; alter database postgres owner to postgres;"
112+
when: debpkg_mode or stage2_nix
113+
107114
- name: Execute init SQL files
108115
become: yes
109116
become_user: postgres

ansible/tasks/setup-postgres.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@
199199
- name: Initialize the database
200200
become: yes
201201
become_user: postgres
202-
shell: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access"
202+
shell: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access" -o "--username=supabase_admin"
203203
vars:
204204
ansible_command_timeout: 60
205205
when: debpkg_mode
206206

207207
- name: Initialize the database stage2_nix
208208
become: yes
209209
become_user: postgres
210-
shell: source /var/lib/postgresql/.bashrc && /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access"
210+
shell: source /var/lib/postgresql/.bashrc && /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access" -o "--username=supabase_admin"
211211
args:
212212
executable: /bin/bash
213213
environment:

docker/all-in-one/postgres-entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ docker_setup_db() {
226226
docker_setup_env() {
227227
file_env 'POSTGRES_PASSWORD'
228228

229-
file_env 'POSTGRES_USER' 'postgres'
230-
file_env 'POSTGRES_DB' "$POSTGRES_USER"
229+
file_env 'POSTGRES_USER' 'supabase_admin'
230+
file_env 'POSTGRES_DB' 'postgres'
231231
file_env 'POSTGRES_INITDB_ARGS'
232232
: "${POSTGRES_HOST_AUTH_METHOD:=}"
233233

flake.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
flake-utils.lib.eachSystem ourSystems (system:
2222
let
2323
pgsqlDefaultPort = "5435";
24-
pgsqlSuperuser = "postgres";
24+
pgsqlSuperuser = "supabase_admin";
2525
nix2img = nix2container.packages.${system}.nix2container;
2626

2727
# The 'oriole_pkgs' variable holds all the upstream packages in nixpkgs, which
@@ -393,7 +393,7 @@
393393
echo '#!/bin/sh' > $TMPDIR/getkey.sh
394394
echo 'echo $PGSODIUM_KEY' >> $TMPDIR/getkey.sh
395395
chmod +x $TMPDIR/getkey.sh
396-
initdb --locale=C
396+
initdb --locale=C --username=supabase_admin
397397
substitute ${./nix/tests/postgresql.conf.in} $PGDATA/postgresql.conf \
398398
--subst-var-by PGSODIUM_GETKEY_SCRIPT "$TMPDIR/getkey.sh"
399399
echo "listen_addresses = '*'" >> $PGDATA/postgresql.conf

migrations/db/init-scripts/00000000000000-initial-schema.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
create publication supabase_realtime;
66

77
-- Supabase super admin
8-
create user supabase_admin;
98
alter user supabase_admin with superuser createdb createrole replication bypassrls;
109

1110
-- Supabase replication user

nix/docker/init.sh.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22
# shellcheck shell=bash
3-
/bin/initdb --locale=C -D /data/postgresql
3+
/bin/initdb --locale=C -D /data/postgresql --username=supabase_admin
44
ln -s /etc/postgresql.conf /data/postgresql/postgresql.conf
55
/bin/postgres -p @PGSQL_DEFAULT_PORT@ -D /data/postgresql

nix/init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
# shellcheck shell=bash
33

4-
export PGUSER=postgres
4+
export PGUSER=supabase_admin
55
export PGDATA=$PWD/postgres_data
66
export PGHOST=$PWD/postgres
77
export PGPORT=5432

nix/tools/migrate-tool.sh.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ echo "NOTE: using temporary directory $DATDIR for PSQL $1 data, which will not b
5959
echo "NOTE: you are free to re-use this data directory at will"
6060
echo
6161

62-
$OLDVER/bin/initdb -D "$DATDIR" --locale=C
63-
$NEWVER/bin/initdb -D "$NEWDAT" --locale=C
62+
$OLDVER/bin/initdb -D "$DATDIR" --locale=C --username=supabase_admin
63+
$NEWVER/bin/initdb -D "$NEWDAT" --locale=C --username=supabase_admin
6464

6565
# NOTE (aseipp): we need to patch postgresql.conf to have the right pgsodium_getkey script
6666
PSQL_CONF_FILE=@PSQL_CONF_FILE@

0 commit comments

Comments
 (0)