﻿# Updating PostgreSQL container 12 and lower to version 18

> [HTML Version](upgrading-postgresql-container-10-to-15.html)

When updating PostgreSQL to a major version, it is necessary to update the version of the running pod to a later version of PostgreSQL. It is also necessary to recreate the storage to update the data stored in the PostgreSQL data files.



Updating a PostgreSQL container consists of five steps:

1. [Database backup](#database-backup).

2. [Installation of PostgreSQL 18 in a Kubernetes cluster](#install-postgresql15).

3. [Recreation of storage](#re-creating-repository).

4. [Starting PostgreSQL](#start-postgresql).

5. [Database recovery](#database-recovery).

## Step 1. Database backup

Before carrying out any actions related to the update, create a backup of the PostgreSQL data and configuration. Read more about how to do this in [Backup and recover databases](database-backup-and-recovery.md).

````
начало внимание

````
If there are third-party databases on the PostgreSQL server, backups of these databases should also be created.

````
конец внимание

````
Backup of the PostgreSQL database without using the brix365-backupper utility

1. Redirect the port to the container with PostgreSQL:

````
kubectl port-forward -n <<namespace>> pods/postgres-0 5477:5432

2. ````
Create an \[OBJECT\] database backup using th \[OBJECT\] utility:

````
pg\_dump -h localhost -p 5477 -U postgres -W brix365 -Fc > path/to/brix365.bak 
````

## ````
Step 2. Installation of PostgreSQL 18 in a Kubernetes cluster

1. Stop the container: \[OBJECT\]:

````
kubectl scale -n <<namespace>> statefulset -l app.kubernetes.io/instance=<<namespace>>,app.kubernetes.io/name=postgresql --replicas 0

2. ````
Save the \[OBJECT\] configuration of the \[OBJECT\] pod to the file \[OBJECT\]:

````
kubectl get statefulsets -n <<namespace>> -lapp.kubernetes.io/instance=<<namespace>>,app.kubernetes.io/name=postgresql -o yaml > path/to/postgresql.yaml

3. ````
Open the \[OBJECT\]  file by using the command:

````
sudo nano path/to/postgresql.yaml

````
In this file, replace the version \[OBJECT\] with version \[OBJECT\].

4. Apply the changes for pod \[OBJECT\]:

````
kubectl apply -f path/to/postgresql.yaml

## ````
Step 3. Recreation of storage

Delete storage \[OBJECT\]:

````
kubectl delete pvc -n <<namespace>> -l app.kubernetes.io/instance=<<namespace>>,app.kubernetes.io/name=postgresql --wait=false

## ````
Step 4. Starting PostgreSQL

Start the container \[OBJECT\]:

````
kubectl scale -n <<namespace>> statefulset -l app.kubernetes.io/instance=<<namespace>>,app.kubernetes.io/name=postgresql --replicas 1

## ````
Step 5. Database recovery

Read more about how to do this in [Backup and recover databases](database-backup-and-recovery.md).

Recovery of the PostgreSQL database without using the brix365-backupper utility

1. Redirect the port to the container with PostgreSQL using the command:

````
kubectl port-forward -n <<namespace>> pods/postgres-0 5477:5432 

2. ````
Restore the \[OBJECT\] database using the  \[OBJECT\] utility:

````
pg\_restore -h localhost -p 5477 -U postgres -W -Fc -d brix365 < brix365.bak
````