Skip to content

Commit 50fdee8

Browse files
committed
Adapt to PostgreSQL >= 18
See docker-library/postgres#1259
1 parent 4f55245 commit 50fdee8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Services/PostgreSQL.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
### Creating the directories to store the PostgreSQL data and the PostgreSQL environment variables
1414

1515
*Unless you're okay using the default values, you'll need to specify the database name, database user and database user's password in the docker run command. Needless to say that those information are sensitive... The recommended way to treat sensitive information in a docker run command are [Docker Secrets](https://docs.docker.com/engine/swarm/secrets/) but, unfortunately, Docker Secrets can only be used with [Docker Swarm](https://www.sumologic.com/glossary/docker-swarm/) (which I don't use myself).*
16-
*So, in order to avoid typing those information as plain text in the docker run command, I'm going to create my own (kind of) secret files inside the 'env' directory that contains those information. For security reasons, those files will only be viewable and editable by the root account, obviously.*
16+
*So, in order to avoid typing those information as plain text in the docker run command, I'm going to create my own (kind of) secret files inside the 'env' directory that contains those information. For security reasons, those files will only be readable and editable by the root account, obviously.*
1717

1818
```bash
1919
sudo mkdir -p /opt/postgres/{data,env}
@@ -46,7 +46,7 @@ sudo chmod 600 /opt/postgres/env/* && sudo chmod 750 /opt/postgres/env
4646
### Pull and run the container
4747

4848
**Warning:**
49-
Upgrading postgres from one major version to another isn't a transparent operation
49+
Upgrading postgres from one major version to another isn't a straightforward operation
5050

5151
New major releases come with structure changes that imply a manual intervention for a seamingless update.
5252
Also, you want to make sure the application using your postgres database is compatible with the new major postgres release yet before upgrading.
@@ -55,8 +55,11 @@ As such, I advise you to **not** use the `latest` tag but to point to an explici
5555

5656
See [Upgrade postgres from one major release to another](#upgrade-postgres-from-one-major-release-to-another) for more details.
5757

58+
**Important note:** Since PostgreSQL >= 18, the volume for the databases data is expected to point to `/var/lib/postgresql` within the container (as opposed to `/var/lib/postgresql/data` with < 18). If you're aiming at deploying a container for PostgreSQL < 18, change the mount point accordingly in the below commands. Make sure to also take this path change into account when upgrading from PostgreSQL < 18 to >= 18.
59+
See https://github.com/docker-library/postgres/pull/1259 for more details.
60+
5861
```bash
59-
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql/data --restart=unless-stopped postgres:15
62+
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql --restart=unless-stopped postgres:18
6063
```
6164

6265
### Set an automatic backup of the database (optional)
@@ -70,7 +73,7 @@ There's multiple ways to automate that backup process. You can simply put the ab
7073

7174
Personally, I use an Ansible Playbook that does the dump and delete every dump older than 7 days.
7275
This Ansible Playbook is launched automatically each day by my Jenkins instance so it performs one dump a day and keep 7 days of dump.
73-
You can see that Ansible Playbook [here](https://github.com/Antiz96/Linux-Server/blob/main/Ansible/playbooks/roles/dump_db/tasks/main.yml).
76+
You can see that Ansible Playbook [here](https://github.com/Antiz96/Linux-Server/blob/main/Ansible/playbooks/roles/dump_databases/tasks/main.yml).
7477

7578
To restore a dump, you can use the following command:
7679

@@ -82,7 +85,7 @@ sudo cat "path_to_the_dump" | sudo docker exec -i postgres psql -U $(sudo cat /o
8285

8386
1 - Stop services using the postgres database, so data are not being written anymore.
8487

85-
2 - Make a proper backup of the current state of the machine running the postgreSQL container (e.g. a snapshot of the VM).
88+
2 - Make a backup or recovery point of the current state of the machine running the postgreSQL container (e.g. a snapshot of the VM).
8689

8790
3 - Perform a dump of the database:
8891

@@ -105,7 +108,7 @@ sudo find /opt/postgres/data -mindepth 1 -delete
105108
6 - Deploy the new container using the new major release as a tag:
106109

107110
```bash
108-
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql/data --restart=unless-stopped postgres:16
111+
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql --restart=unless-stopped postgres:19
109112
```
110113

111114
7 - Restore the dump you created earlier:
@@ -125,15 +128,15 @@ Since we use Docker, the update and upgrade procedure is actually the same as it
125128
(... to check if there's available updates)
126129

127130
```bash
128-
sudo docker pull postgres:15 # Adapt the tag to the one you're currently using
131+
sudo docker pull postgres:18 # Adapt the tag to the one you're currently using
129132
```
130133

131134
### Apply the update
132135

133136
```bash
134137
sudo docker stop postgres
135138
sudo docker rm postgres
136-
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql/data --restart=unless-stopped postgres:15 # Adapt the tag to the one you're currently using
139+
sudo docker run -d --name postgres -p 5432:5432 -e POSTGRES_USER=$(sudo cat /opt/postgres/env/user) -e POSTGRES_PASSWORD=$(sudo cat /opt/postgres/env/password) -e POSTGRES_DB=$(sudo cat /opt/postgres/env/database) -v /opt/postgres/data:/var/lib/postgresql --restart=unless-stopped postgres:15 # Adapt the tag to the one you're currently using
137140
```
138141

139142
### After an update

0 commit comments

Comments
 (0)