Upgrade PostgreSQL de 11 para 12 (em Debian 10)

Confira a versão instalada:

ricardo@PostgreSQL:~$ apt list --installed | grep postgresql

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

postgresql-11/stretch-pgdg,now 11.6-1.pgdg90+1 amd64 [installed]
postgresql-client-11/stretch-pgdg,now 11.6-1.pgdg90+1 amd64 [installed,automatic]
postgresql-client-common/stretch-pgdg,now 210.pgdg90+1 all [installed,automatic]
postgresql-common/stretch-pgdg,now 210.pgdg90+1 all [installed,automatic]
ricardo@PostgreSQL:~$

Instale a nova versão:

ricardo@PostgreSQL:~$ sudo apt update
ricardo@PostgreSQL:~$ sudo apt install postgresql-12

Confira as diferenças entre os arquivos de configuração, e adeque o novo (mantenha a porta da nova versão diferente da velha versão, que provavelmente é a 5432):

ricardo@PostgreSQL:~$ sudo diff /etc/postgresql/11/main/postgresql.conf /etc/postgresql/12/main/postgresql.conf
ricardo@PostgreSQL:~$ sudo diff /etc/postgresql/11/main/pg_hba.conf /etc/postgresql/12/main/pg_hba.conf

Pare o PostgreSQL:

ricardo@PostgreSQL:~$ sudo systemctl stop postgresql

Execute o upgrade com a opção

--check

, para conferir se tudo está ok:

ricardo@PostgreSQL:~$ sudo su - postgres
postgres@PostgreSQL:~$ /usr/lib/postgresql/12/bin/pg_upgrade --old-datadir=/var/lib/postgresql/11/main --new-datadir=/var/lib/postgresql/12/main --ew-bindir=/usr/lib/postgresql/12/bin --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' --check
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

*Clusters are compatible*
postgres@PostgreSQL:~$

Agora sim é o Upgrade para valer:

postgres@PostgreSQL:~$ /usr/lib/postgresql/12/bin/pg_upgrade --old-datadir=/var/lib/postgresql/11/main --new-datadir=/var/lib/postgresql/12/main --ew-bindir=/usr/lib/postgresql/12/bin --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
postgres@PostgreSQL:~$

Altere a porta da nova versão para a que era usada na velha versão, e inicie o PostgreSQL:

ricardo@PostgreSQL:~$ sudo vim /etc/postgresql/12/main/postgresql.conf
ricardo@PostgreSQL:~$ sudo systemctl start postgresql

Execute o ANALYZE:

ricardo@PostgreSQL:~$ sudo su - postgres
postgres@PostgreSQL:~$ sh analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.

If you would like default statistics as quickly as possible, cancel
this script and run:
    "/usr/lib/postgresql/12/bin/vacuumdb" --all --analyze-only

vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

Done
postgres@PostgreSQL:~$

Se está tudo funcionando bem na nova versão, remova a versão antiga:

ricardo@PostgreSQL:~$ sudo apt remove postgresql-11 postgresql-client-11
...
ricardo@PostgreSQL:~$ sudo rm -rf /etc/postgresql/11/
ricardo@PostgreSQL:~$ sudo su - postgres
postgres@PostgreSQL:~$ sh delete_old_cluster.sh
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/11/
total 0
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/12
total 4,0K
drwx------ 19 postgres postgres 4,0K jan 25 09:27 main
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/11/
total 0
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/12
total 4,0K
drwx------ 19 postgres postgres 4,0K jan 25 09:27 main
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/12/,
ls: não foi possível acessar '/var/lib/postgresql/12/,': Arquivo ou diretório não encontrado
postgres@PostgreSQL:~$ ls -lh /var/lib/postgresql/12/main/
total 84K
drwx------ 5 postgres postgres 4,0K jan 25 09:26 base
drwx------ 2 postgres postgres 4,0K jan 25 16:49 global
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_commit_ts
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_dynshmem
drwx------ 4 postgres postgres 4,0K jan 25 16:47 pg_logical
drwx------ 4 postgres postgres 4,0K jan 25 09:26 pg_multixact
drwx------ 2 postgres postgres 4,0K jan 25 09:27 pg_notify
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_replslot
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_serial
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_snapshots
drwx------ 2 postgres postgres 4,0K jan 25 09:27 pg_stat
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_stat_tmp
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_subtrans
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_tblspc
drwx------ 2 postgres postgres 4,0K jan 25 09:18 pg_twophase
-rw------- 1 postgres postgres    3 jan 25 09:18 PG_VERSION
drwx------ 3 postgres postgres 4,0K jan 25 09:26 pg_wal
drwx------ 2 postgres postgres 4,0K jan 25 09:26 pg_xact
-rw------- 1 postgres postgres   88 jan 25 09:18 postgresql.auto.conf
-rw------- 1 postgres postgres  130 jan 25 09:27 postmaster.opts
-rw------- 1 postgres postgres  108 jan 25 09:27 postmaster.pid
postgres@PostgreSQL:~$

E confira que somente a nova versão está instalada:

ricardo@PostgreSQL:~$ apt list --installed | grep postgresql

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

postgresql-11/stretch-pgdg,now 11.6-1.pgdg90+1 amd64 [installed]
postgresql-12/stretch-pgdg,now 12.1-1.pgdg90+1 amd64 [installed]
postgresql-client-11/stretch-pgdg,now 11.6-1.pgdg90+1 amd64 [installed,automatic]
postgresql-client-12/stretch-pgdg,now 12.1-1.pgdg90+1 amd64 [installed,automatic]
postgresql-client-common/stretch-pgdg,now 210.pgdg90+1 all [installed,automatic]
postgresql-common/stretch-pgdg,now 210.pgdg90+1 all [installed,automatic]
ricardo@PostgreSQL:~$

Leave a Reply

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Esse site utiliza o Akismet para reduzir spam. Aprenda como seus dados de comentários são processados.