PostgreSQL’de Nasıl Sürüm Yükseltilir?

Debian bir işletim sisteminde öncelikli olarak kurulacak güncel postgresql sürümü belirlenir, ben bu makaleyi kaleme alırken en güncel sürüm 13.2 olduğu için Postgresql repolarını direk sisteme dahil edeceğim.

Eğer Debian repolarını kullanırsam maksimum 12 sürümünü kullanabileceğim ki bu istediğim bir şey değil çünkü 13 sürümündeki bir takım özellikler gerekiyor ve bu özellikler hayat kurtaracak.

sudo sh -c 'echo "deb <http://apt.postgresql.org/pub/repos/apt> $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - <https://www.postgresql.org/media/keys/ACCC4CF8.asc> | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql-13

Bağımlılık kontrolü yapalım eksik bir şey kalmasını istemeyiz.

apt list --installed | grep postgresql

Hemen sonrasında eski sürüm yapılandırmalar’da yer alan değişikliklere göz atıyoruz.

diff /etc/postgresql/11/main/postgresql.conf /etc/postgresql/13/main/postgresql.conf
diff /etc/postgresql/11/main/pg_hba.conf /etc/postgresql/13/main/pg_hba.conf

Postgresql’in tüm servislerini durduruyoruz, sistem üzerinde kurulu olan her iki sürümüde kapatmalıyız.

sudo systemctl stop postgresql.service

Postgresql kullanıcısı ile giriş yapmalıyız

sudo su postgres

Postgresql migration işlemini gerçekleştirebilmemiz için pg_upgrade uygulamasını çalıştırmalıyız. Bunuda aşağıdaki gibi yapabilirsiniz.

/usr/lib/postgresql/13/bin/pg_upgrade \\
  --old-datadir=/var/lib/postgresql/11/main \\
  --new-datadir=/var/lib/postgresql/13/main \\
  --old-bindir=/usr/lib/postgresql/11/bin \\
  --new-bindir=/usr/lib/postgresql/13/bin \\
  --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' \\
  --new-options '-c config_file=/etc/postgresql/13/main/postgresql.conf' \\
  --check

Kontrolleri sağlanacak biraz sürebilir, kontroller tamamlandıysa ve hiç bir hata yoksa aşağıdaki gibi bir çıktı sizi karşılar

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
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

Ardından --check parametresini kaldırıp işlemi başlatıyoruz.

/usr/lib/postgresql/13/bin/pg_upgrade \\
  --old-datadir=/var/lib/postgresql/11/main \\
  --new-datadir=/var/lib/postgresql/13/main \\
  --old-bindir=/usr/lib/postgresql/11/bin \\
  --new-bindir=/usr/lib/postgresql/13/bin \\
  --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' \\
  --new-options '-c config_file=/etc/postgresql/13/main/postgresql.conf'

Bu işlem veritabanı boyutunuza göre değişebilir. Indexlerin alınmayacağını unutmayın. Tamamlandığında aşağıdaki gibi bir çıktı gene sizi bekliyor.

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
Checking for new cluster tablespace directories             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

Ardından servisleri root kullanıcısı ile başlatıyoruz.

systemctl start postgresql@13-main.service

Her şey olması gerektiği gibi mi diye postgresql üzerinde analiz için vacuum işlemini tetikliyoruz. Bu işlemi postgres kullanıcısı ile yapmayı unutmayın.

./analyze_new_cluster.sh

Çıktısı şu şekilde olmalı

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/13/bin/vacuumdb" --all --analyze-only

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

Done

Ardından eski data klasörünü kaldıralım.

./delete_old_cluster.sh

Bu işlemler sorunucunda sistemde çift veritabanı sürümü olacağını unutmayın kullanmadığınız sürümü kalıcı olarak kaldırmanızı şiddetle öneririm.

sudo apt-get remove postgresql-11
sudo rm -rf /etc/postgresql/11/

Sistem Uzmanı, Linux Hacısı, El-Kernel

Yorum yapın