Upgrade to PostgreSQL 12 on FreeBSD

This is a straight forward guide for safely upgrade from PostgreSQL 11.6 to 12.4

First take a snapshot or save the entire virtual machine.

First get some information about the environment on the virtual machine.

pkg info | grep sql
postgresql11-client-11.6       PostgreSQL database (client)
postgresql11-server-11.6       PostgreSQL is the most advanced open-source database available anywhere

Show Versions of the PostgreSQL databases

/usr/local/bin/psql -U pgsql -d dbwnk2016 -t --command "show server_version"
11.6

List all databases

psql -U pgsql -d dbwnk2016 -l
                          List of databases
    Name     | Owner | Encoding | Collate | Ctype | Access privileges 
-------------+-------+----------+---------+-------+-------------------
 postgres    | pgsql | UTF8     | C       | C     | 
 template0   | pgsql | UTF8     | C       | C     | =c/pgsql         +
 template1   | pgsql | UTF8     | C       | C     | =c/pgsql         +
 dbwnk2016   | pgsql | UTF8     | C       | C     | 
(4 rows)

In our case we only need to upgrade database "dbwnk2016".

Take a dump of the whole PostgreSQL databases

/usr/local/bin/pg_dump --create --encoding=UTF8 --column-inserts --dbname=dbwnk2016 \
         --format=p --inserts  --username=postgres \
         --file=/usr/local/www/wnk20200821.sql

Check size of the exported data and ddl

ls -altr /usr/local/www/*.sql -rw-r--r-- 1 root wheel 161881860 Aug 21 12:59 /usr/local/www/wnk20200821.sql

Update PostgreSQL default version in FreeBSD config files

At the time of writing the default version for postgreSQL was version 9.3. However, we need version 9.6.

vi /usr/ports/Mk/bsd.default-versions.mk

Change the following values

# Possible values: 9.2, 9.3, 9.4, 9.5, 9.6 PGSQL_DEFAULT?= 9.6

Stop PostgreSQL 11 service and delete binaries

service postgresql stop

delete postgreSQL binaries

pkg delete -fy postgresql11-server-11.6
Checking integrity... done (0 conflicting)
Deinstallation has been requested for the following 1 packages (of 0 packages in the universe):

Installed packages to be REMOVED:
        postgresql11-server: 11.6

Number of packages to be removed: 1

The operation will free 22 MiB.
[1/1] Deinstalling postgresql11-server-11.6...
[1/1] Deleting files for postgresql11-server-11.6: 100%
==> You should manually remove the "postgres" user. 
==> You should manually remove the "postgres" group 
pkg delete -fy postgresql11-client-11.6
Checking integrity... done (0 conflicting)
Deinstallation has been requested for the following 1 packages (of 0 packages in the universe):

Installed packages to be REMOVED:
        postgresql11-client: 11.6

Number of packages to be removed: 1

The operation will free 12 MiB.
[1/1] Deinstalling postgresql11-client-11.6...
[1/1] Deleting files for postgresql11-client-11.6: 100%

We save to old environment data

doesnt wrok /usr/local/pgsql /usr/local/pgsql20200821.old; ls -altr /usr/local/pgsql20200821.old



Install PostgreSQL 11

The installation of the PostgreSQL server software also installs the PostgreSQL client

pkg install postgresql12-server
Updating FreeBSD repository catalogue...
Fetching meta.txz: 100%    916 B   0.9kB/s    00:01    
Fetching packagesite.txz: 100%    6 MiB   6.6MB/s    00:01    
Processing entries: 100%
FreeBSD repository update completed. 31941 packages processed.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
The following 1 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
        pkg: 1.13.2 -> 1.14.6

Number of packages to be upgraded: 1

6 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/1] Fetching pkg-1.14.6.txz: 100%    6 MiB   6.8MB/s    00:01    
Checking integrity... done (0 conflicting)
[1/1] Upgrading pkg from 1.13.2 to 1.14.6...
[1/1] Extracting pkg-1.14.6: 100%
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 3 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        postgresql12-client: 12.3
        postgresql12-server: 12.3

Installed packages to be UPGRADED:
        icu: 65.1,1 -> 67.1,1

Number of packages to be installed: 2
Number of packages to be upgraded: 1

The process will require 35 MiB more space.
17 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/3] Fetching postgresql12-server-12.3.txz: 100%    5 MiB   4.8MB/s    00:01    
[2/3] Fetching icu-67.1,1.txz: 100%   10 MiB  10.6MB/s    00:01    
[3/3] Fetching postgresql12-client-12.3.txz: 100%    3 MiB   2.7MB/s    00:01    
Checking integrity... done (0 conflicting)
[1/3] Upgrading icu from 65.1,1 to 67.1,1...
[1/3] Extracting icu-67.1,1: 100%
[2/3] Installing postgresql12-client-12.3...
[2/3] Extracting postgresql12-client-12.3: 100%
[3/3] Installing postgresql12-server-12.3...
===> Creating groups.
Using existing group 'postgres'.
===> Creating users
Using existing user 'postgres'.
===> Creating homedir(s)

  =========== BACKUP YOUR DATA! =============
  As always, backup your data before
  upgrading. If the upgrade leads to a higher
  major revision (e.g. 9.6 -> 10), a dump
  and restore of all databases is
  required. This is *NOT* done by the port!
  See https://www.postgresql.org/docs/current/upgrading.html
  ===========================================
[3/3] Extracting postgresql12-server-12.3: 100%
=====
Message from postgresql12-client-12.3:

--
The PostgreSQL port has a collection of "side orders":

postgresql-docs
  For all of the html documentation

p5-Pg
  A perl5 API for client access to PostgreSQL databases.

postgresql-tcltk 
  If you want tcl/tk client support.

postgresql-jdbc
  For Java JDBC support.

postgresql-odbc
  For client access from unix applications using ODBC as access
  method. Not needed to access unix PostgreSQL servers from Win32
  using ODBC. See below.

ruby-postgres, py-psycopg2
  For client access to PostgreSQL databases using the ruby & python
  languages.

postgresql-plperl, postgresql-pltcl & postgresql-plruby
  For using perl5, tcl & ruby as procedural languages.

postgresql-contrib
  Lots of contributed utilities, postgresql functions and
  datatypes. There you find pg_standby, pgcrypto and many other cool
  things.

etc...
=====
Message from postgresql12-server-12.3:

--
For procedural languages and postgresql functions, please note that
you might have to update them when updating the server.

If you have many tables and many clients running, consider raising
kern.maxfiles using sysctl(8), or reconfigure your kernel
appropriately.

The port is set up to use autovacuum for new databases, but you might
also want to vacuum and perhaps backup your database regularly. There
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
you may find useful. You can use it to backup and perform vacuum on all
databases nightly. Per default, it performs `vacuum analyze'. See the
script for instructions. For autovacuum settings, please review
~pgsql/data/postgresql.conf.

If you plan to access your PostgreSQL server using ODBC, please
consider running the SQL script /usr/local/share/postgresql/odbc.sql
to get the functions required for ODBC compliance.

Please note that if you use the rc script,
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
(UTF-8) will be used to store character data by default.  Set
postgresql_initdb_flags or use login.conf settings described below to
alter this behaviour. See the start rc script for more info.

To set limits, environment stuff like locale and collation and other
things, you can set up a class in /etc/login.conf before initializing
the database. Add something similar to this to /etc/login.conf:
---
postgres:\
        :lang=en_US.UTF-8:\
        :setenv=LC_COLLATE=C:\
        :tc=default:
---
and run `cap_mkdb /etc/login.conf'.
Then add 'postgresql_class="postgres"' to /etc/rc.conf.

======================================================================

To initialize the database, run

  /usr/local/etc/rc.d/postgresql initdb

You can then start PostgreSQL by running:

  /usr/local/etc/rc.d/postgresql start

For postmaster settings, see ~pgsql/data/postgresql.conf

NB. FreeBSD's PostgreSQL port logs to syslog by default
    See ~pgsql/data/postgresql.conf for more info

NB. If you're not using a checksumming filesystem like ZFS, you might
    wish to enable data checksumming. It can only be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgres_initdb_flags rcvar.  Check the initdb(1) manpage
    for more info and make sure you understand the performance
    implications.

======================================================================

To run PostgreSQL at startup, add
'postgresql_enable="YES"' to /etc/rc.conf
/usr/local/etc/rc.d/postgresql initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data12 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/bin/pg_ctl -D /var/db/postgres/data12 -l logfile start

Let's start the database

service postgresql start
2020-08-21 13:15:39.438 UTC [91900] LOG:  starting PostgreSQL 12.3 on amd64-portbld-freebsd12.1, compiled by FreeBSD clang version 8.0.1 (tags/RELEASE_801/final 366581) (based on LLVM 8.0.1), 64-bit
2020-08-21 13:15:39.440 UTC [91900] LOG:  listening on IPv6 address "::1", port 5432
2020-08-21 13:15:39.440 UTC [91900] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-08-21 13:15:39.442 UTC [91900] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-08-21 13:15:39.462 UTC [91900] LOG:  ending log output to stderr
2020-08-21 13:15:39.462 UTC [91900] HINT:  Future log output will go to log destination "syslog".

Restore the exported databases

su postgres psql -f /usr/local/www/wnk20200821.sql > /var/log/www/wnk20200821crtdb.log create role xyz with login; grant usage on schema lf24.to lf24. psql -l dbwnk2016

Reboot Virtual Maching

shutdown -r now

Cleanup

After successful upgrade you should delete the snapshot of the virtual machine and delete the files in /var/db/postgres/data11.

Published: Aug. 21, 2020

Change Content

The information on this page is user generated content. The content does not claim to be complete or correct.

Everybody is invited to add or change the data. Just click on this link. No login or email is required. Thanks.