-
Notifications
You must be signed in to change notification settings - Fork 230
PMM 15014 Upgrade internal PostgreSQL version from 14 to 18 #5295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
89ec787
PMM-7 Upgrade internal PostgreSQL from 14 to 18.
talhabinrizwan 31745a5
PMM-7 Upgrade internal PostgreSQL from 14 to 18.
talhabinrizwan db280b1
Grant CREATE ON SCHEMA public to grafana and pmm-managed users
talhabinrizwan 01a7a31
Fix permission denied on public schema for PostgreSQL 15+
talhabinrizwan 439689a
Grant CREATE ON SCHEMA public before running migrations
talhabinrizwan 17bf090
Restore scram-sha-256 pg_hba entry for dev container
talhabinrizwan a4374dd
PMM-15014 Remove max_connections from pmm_config.go template
talhabinrizwan 803a9e4
Merge branch 'main' into PMM-15014-upgrade-to-pg18
ademidoff e712e3e
PMM-15014 Document the PostgreSQL 14 to 18 migration
ademidoff f3dfe58
PMM-15014 Grant schema access through database ownership
ademidoff 9061044
PMM-15014 Move embedded cluster creation into postgres-migration
ademidoff eb125aa
PMM-15014 Drop the pg_hba trust rewrite
ademidoff b1dff33
PMM-15014 Temporarily lower minPGVersion to 14
ademidoff 7cdda0c
PMM-15014 Temporarily expect postgresql14.log in TestFiles
ademidoff 9a4748a
PMM-15014 Name the PostgreSQL log file without the major version
ademidoff a0d070f
PMM-15014 Shorten the counter variable names in initWithRoot
ademidoff 6326518
PMM-15014 Quote identifiers and the password in initWithRoot
ademidoff 7e21ec6
PMM-15014 Fix the pg14 upgrade on data directories from PMM 2
ademidoff d82c74c
PMM-15014 Remove the upgrade dumps once the migration completes
ademidoff ce54fef
PMM-15014 Build the new cluster in a staging directory
ademidoff d4e5517
Merge branch 'main' into PMM-15014-upgrade-to-pg18
ademidoff d462e79
Merge branch 'main' into PMM-15014-upgrade-to-pg18
ademidoff 617af31
PMM-15014 Keep minPGVersion at 14
ademidoff 87ae436
PMM-15014 Expect postgresql.log in TestFiles
ademidoff 1bb05db
PMM-15014 Restore the v11 to v14 dumps with psql
ademidoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,105 @@ | |
| set -o errexit | ||
| set -o pipefail | ||
|
|
||
| declare POSTGRES_DATA_DIR="${POSTGRES_DATA_DIR:-/srv/postgres14}" | ||
| declare POSTGRES_DATA_DIR="${POSTGRES_DATA_DIR:-/srv/postgres18}" | ||
| declare POSTGRES_PASSWORD_FILE="${POSTGRES_PASSWORD_FILE:-/srv/.postgres_password}" | ||
|
|
||
| # ── pg14 → pg18 upgrade ─────────────────────────────────────────────────────── | ||
| # Runs once when /srv/postgres14 exists and /srv/postgres18 does not. | ||
| # Requires both pg14 and pg18 binaries in the image (shipped through PMM 3.12.x). | ||
| upgrade_pg14_to_pg18() { | ||
| local PG14_DATA="/srv/postgres14" | ||
| local PG14_BIN="/usr/pgsql-14/bin" | ||
| local PG18_BIN="/usr/pgsql-18/bin" | ||
| local BACKUP_DIR="/srv/backup" | ||
|
|
||
| if [ ! -d "$PG14_DATA" ] || [ -d "$POSTGRES_DATA_DIR" ]; then | ||
| return | ||
| fi | ||
|
|
||
| echo "PostgreSQL 14 data found at $PG14_DATA. Migrating to PostgreSQL 18..." | ||
|
|
||
| local PGPASSWORD | ||
| PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") | ||
| export PGPASSWORD | ||
|
|
||
| # Start pg14 and dump databases | ||
| echo "Starting PostgreSQL 14 for dump..." | ||
| "$PG14_BIN/pg_ctl" start -D "$PG14_DATA" -o "-c logging_collector=off" -w | ||
|
|
||
| mkdir -p "$BACKUP_DIR" | ||
|
|
||
| echo "Dumping pmm-managed database..." | ||
| "$PG14_BIN/pg_dump" -h /run/postgresql -U postgres -F p \ | ||
| -f "${BACKUP_DIR}/pg18-upgrade-pmm-managed.sql" pmm-managed | ||
|
|
||
| # Skip grafana dump when an external database is configured (HA mode). | ||
| if [ -z "${GF_DATABASE_URL}" ] && [ -z "${GF_DATABASE_HOST}" ]; then | ||
| echo "Dumping grafana database..." | ||
| "$PG14_BIN/pg_dump" -h /run/postgresql -U postgres -F p \ | ||
| -f "${BACKUP_DIR}/pg18-upgrade-grafana.sql" grafana | ||
| fi | ||
|
|
||
| echo "Stopping PostgreSQL 14..." | ||
| "$PG14_BIN/pg_ctl" stop -D "$PG14_DATA" -w | ||
|
|
||
| unset PGPASSWORD | ||
|
|
||
| # Initialise pg18 | ||
| echo "Initialising PostgreSQL 18 at $POSTGRES_DATA_DIR..." | ||
| install -d -m 750 "$POSTGRES_DATA_DIR" | ||
| "$PG18_BIN/initdb" -D "$POSTGRES_DATA_DIR" \ | ||
| --auth-host=scram-sha-256 \ | ||
| --auth-local=trust \ | ||
| --username=postgres \ | ||
| --pwfile="$POSTGRES_PASSWORD_FILE" | ||
|
|
||
| # Start pg18 and restore | ||
| echo "Starting PostgreSQL 18..." | ||
| "$PG18_BIN/pg_ctl" start -D "$POSTGRES_DATA_DIR" -o "-c logging_collector=off" -w | ||
|
|
||
| PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") | ||
| export PGPASSWORD | ||
|
|
||
| echo "Creating pmm-managed role and database..." | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d postgres \ | ||
| -c "CREATE ROLE \"pmm-managed\" LOGIN PASSWORD 'pmm-managed'" | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d postgres \ | ||
| -c "CREATE DATABASE \"pmm-managed\" OWNER \"pmm-managed\"" | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d pmm-managed \ | ||
| -c "GRANT CREATE ON SCHEMA public TO \"pmm-managed\"" | ||
| echo "Restoring pmm-managed database..." | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d pmm-managed \ | ||
| -f "${BACKUP_DIR}/pg18-upgrade-pmm-managed.sql" | ||
|
|
||
| if [ -f "${BACKUP_DIR}/pg18-upgrade-grafana.sql" ]; then | ||
| echo "Creating grafana role and database..." | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d postgres \ | ||
| -c "CREATE ROLE grafana LOGIN PASSWORD 'grafana'" | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d postgres \ | ||
| -c "CREATE DATABASE grafana OWNER grafana" | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d grafana \ | ||
| -c "GRANT CREATE ON SCHEMA public TO grafana" | ||
| echo "Restoring grafana database..." | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d grafana \ | ||
| -f "${BACKUP_DIR}/pg18-upgrade-grafana.sql" | ||
| fi | ||
|
|
||
| echo "Ensuring pg_stat_statements extension..." | ||
| "$PG18_BIN/psql" -h /run/postgresql -U postgres -d postgres \ | ||
| -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements SCHEMA public" | ||
|
|
||
| echo "Stopping PostgreSQL 18..." | ||
| "$PG18_BIN/pg_ctl" stop -D "$POSTGRES_DATA_DIR" -w | ||
|
|
||
| unset PGPASSWORD | ||
|
|
||
| echo "Renaming $PG14_DATA to ${PG14_DATA}.old for rollback safety..." | ||
| mv "$PG14_DATA" "${PG14_DATA}.old" | ||
|
|
||
| echo "PostgreSQL 14 to 18 migration completed successfully." | ||
| } | ||
|
|
||
| ensure_postgres_password() { | ||
| # This check is to verify if the data directory is empty. | ||
| if [ ! -f "$POSTGRES_DATA_DIR/PG_VERSION" ]; then | ||
|
|
@@ -20,8 +116,8 @@ ensure_postgres_password() { | |
| echo "Generating postgres superuser password..." | ||
| POSTGRES_PASSWORD=$(openssl rand -hex 16) | ||
|
|
||
| if ! /usr/pgsql-14/bin/pg_ctl status -D "$POSTGRES_DATA_DIR" > /dev/null 2>&1; then | ||
| /usr/pgsql-14/bin/pg_ctl start -D "$POSTGRES_DATA_DIR" -o "-c logging_collector=off" | ||
| if ! /usr/pgsql-18/bin/pg_ctl status -D "$POSTGRES_DATA_DIR" > /dev/null 2>&1; then | ||
|
ademidoff marked this conversation as resolved.
Outdated
|
||
| /usr/pgsql-18/bin/pg_ctl start -D "$POSTGRES_DATA_DIR" -o "-c logging_collector=off" | ||
| STARTED=1 | ||
| fi | ||
|
|
||
|
|
@@ -32,13 +128,13 @@ ensure_postgres_password() { | |
| chmod 600 "$POSTGRES_PASSWORD_FILE" | ||
|
|
||
| if [ "$STARTED" -eq 1 ]; then | ||
| /usr/pgsql-14/bin/pg_ctl stop -D "$POSTGRES_DATA_DIR" | ||
| /usr/pgsql-18/bin/pg_ctl stop -D "$POSTGRES_DATA_DIR" | ||
| fi | ||
| } | ||
|
|
||
| update_pg_hba_auth() { | ||
| local hba="$POSTGRES_DATA_DIR/pg_hba.conf" | ||
|
|
||
| if [ ! -f "$hba" ]; then | ||
| return 1 | ||
| fi | ||
|
|
@@ -53,10 +149,11 @@ update_pg_hba_auth() { | |
| sed -E 's/^([[:space:]]*host[[:space:]].*[[:space:]])trust([[:space:]]*)$/\1scram-sha-256\2/' "$hba" > "$tmp" | ||
| mv "$tmp" "$hba" | ||
|
|
||
| if /usr/pgsql-14/bin/pg_ctl status -D "$POSTGRES_DATA_DIR" > /dev/null 2>&1; then | ||
| /usr/pgsql-14/bin/pg_ctl reload -D "$POSTGRES_DATA_DIR" | ||
| if /usr/pgsql-18/bin/pg_ctl status -D "$POSTGRES_DATA_DIR" > /dev/null 2>&1; then | ||
| /usr/pgsql-18/bin/pg_ctl reload -D "$POSTGRES_DATA_DIR" | ||
| fi | ||
| } | ||
|
|
||
| upgrade_pg14_to_pg18 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this func uses |
||
| ensure_postgres_password | ||
| update_pg_hba_auth || true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.