-
Notifications
You must be signed in to change notification settings - Fork 297
Upgrade PostgreSQL
ruslandoga edited this page Sep 13, 2024
·
4 revisions
This is a guide to upgrading PostgreSQL version >= 12
to version 16
or later using pg_dump
and psql
.
It is based on Upgrade a PostgreSQL database with docker.
Note: following this guide you'd need to stop some containers and it would make your Plausible instance temporarily unavailable.
- dump contents of the old version PostgreSQL to a file
- copy the dump to the host
- replace old version PostgreSQL with new version PostgreSQL
- copy and load the dump into new version PostgreSQL
- Stop
plausible
to avoid writing to oldplausible_db
> docker compose stop plausible
[+] Running 2/2
⠿ Container hosting-plausible-1 Stopped 6.5s
- Dump old
plausible_db
contents to a backup file
> docker compose exec plausible_db sh -c "pg_dump -U postgres plausible_db > plausible_db.bak"
- Copy the backup to the host
> docker compose cp plausible_db:plausible_db.bak plausible_db.bak
- (Optional) verify backup went OK
> head plausible_db.bak
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.12 (Debian 12.12-1.pgdg110+1)
-- Dumped by pg_dump version 12.12 (Debian 12.12-1.pgdg110+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
- Edit
docker-compose.yml
to use new PostgreSQL version, here we update from version 12 to version 16, alpine flavour.
plausible_db:
- image: postgres:12
+ image: postgres:16-alpine
restart: always
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
- Ensure relevant containers are stopped
> docker compose stop plausible plausible_db
[+] Running 2/2
⠿ Container hosting-plausible-1 Stopped 0.0s
⠿ Container hosting-plausible_db-1 Stopped 0.2s
- Remove old
plausible_db
container to be able to nuke its volume in the next step
> docker compose rm plausible_db
? Going to remove hosting-plausible_db-1 Yes
[+] Running 1/0
⠿ Container hosting-plausible_db-1 Removed 0.0s
- Remove old
plausible_db
volume, mine is namedhosting_db-data
> docker volume ls
DRIVER VOLUME NAME
<...snip...>
local hosting_db-data
local hosting_event-data
<...snip...>
> docker volume rm hosting_db-data
hosting_db-data
- Start new version
plausible_db
container
> docker compose up plausible_db -d
[+] Running 9/9
⠿ plausible_db Pulled 9.3s
⠿ 9b18e9b68314 Already exists 0.0s
⠿ 75aada9edfc5 Pull complete 1.2s
⠿ 820773693750 Pull complete 1.2s
⠿ 8812bb04ef2e Pull complete 5.2s
⠿ 2ccec0f7805c Pull complete 5.2s
⠿ 833f9b98598e Pull complete 5.3s
⠿ 1eb578dc04e6 Pull complete 5.4s
⠿ c873bf6204df Pull complete 5.4s
[+] Running 2/2
⠿ Volume "hosting_db-data" Created 0.0s
⠿ Container hosting-plausible_db-1 Started 0.5s
- Create new DB and load data into it
> docker compose exec plausible_db createdb -U postgres plausible_db
> docker compose cp plausible_db.bak plausible_db:plausible_db.bak
> docker compose exec plausible_db sh -c "psql -U postgres -d plausible_db < plausible_db.bak"
SET
SET
SET
SET
SET
set_config
------------
(1 row)
SET
SET
SET
SET
CREATE EXTENSION
<...snip...>
- Start all other containers
> docker compose up -d
[+] Running 4/4
⠿ Container hosting-plausible_events_db-1 Running 0.0s
⠿ Container hosting-plausible_db-1 Started 0.5s
⠿ Container hosting-plausible-1 Started 0.5s
- (Optional) Remove backups from the container and the host
> rm plausible_db.bak
> docker compose exec plausible_db rm plausible_db.bak
Note
Plausible CE is funded by our cloud subscribers. So if you know someone who might find Plausible Cloud useful, please let them know about us!