Upgrade to PostgreSQL 17 on FreeBSD 14

This Howto explains the upgrade to PostgreSQL 17 on FreeBSD 14.

IMPORTANT: First take a snapshot of the virtual machine or save the entire server.

Get Information about the Environment

pkg info | grep sql

postgresql16-client-16.4  PostgreSQL database (client)
postgresql16-server-16.4

Show Version of the PostgreSQL Database

/usr/local/bin/psql -U postgres -d dbname -t --command "show server_version"
16.4

Output the Location of the Configuration Directories

pg_config

BINDIR = /usr/local/bin
DOCDIR = /usr/local/share/doc/postgresql
HTMLDIR = /usr/local/share/doc/postgresql
INCLUDEDIR = /usr/local/include
PKGINCLUDEDIR = /usr/local/include/postgresql
INCLUDEDIR-SERVER = /usr/local/include/postgresql/server
LIBDIR = /usr/local/lib
PKGLIBDIR = /usr/local/lib/postgresql
LOCALEDIR = /usr/local/share/locale
MANDIR = /usr/local/share/man
SHAREDIR = /usr/local/share/postgresql
SYSCONFDIR = /usr/local/etc/postgresql
CC = cc
CPPFLAGS = -I/usr/local/include -I/usr/local/include -I/usr/local/include
CFLAGS_SL = -fPIC -DPIC
LDFLAGS = -L/usr/local/lib -lpthread -L/usr/local/lib -fstack-protector-strong -L/usr/local/lib -Wl,--as-needed -Wl,-R'/usr/local/lib'
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lintl -lssl -lcrypto -lz -lreadline -lexecinfo -lm
VERSION = PostgreSQL 16.4

Get a List of the PostgreSQL Databases

/usr/local/bin/psql -U postgres -d dbname -l

                                                  List of databases
   Name    |  Owner   | Encoding | Locale Provider | Collate |  Ctype  | ICU Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+---------+---------+------------+-----------+-----------------------
 dbname    | postgres | UTF8     | libc            | C       | C.UTF-8 |            |           |
 postgres  | postgres | UTF8     | libc            | C       | C.UTF-8 |            |           |
 template0 | postgres | UTF8     | libc            | C       | C.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |         |         |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | C       | C.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |         |         |            |           | postgres=CTc/postgres
(4 rows)

Dump all PostgreSQL Databases

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

Check if the Dump Exists

ls -altr /usr/local/www/*.sql

Postgresql Data Dir: /var/db/postgres/data17/

Stop The PostgreSQL Service and Remove Old Packages

sudo service postgresql stop sudo pkg delete -fy postgresql16-server-16.4;

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:
        postgresql16-server: 16.4

Number of packages to be removed: 1

The operation will free 51 MiB.
[1/1] Deinstalling postgresql16-server-16.4...
[1/1] Deleting files for postgresql16-server-16.4: 100%
==> You should manually remove the "postgres" user
==> You should manually remove the "postgres" group
sudo pkg delete -fy postgresql16-client-16.4 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: postgresql16-client: 16.4 Number of packages to be removed: 1 The operation will free 15 MiB. [1/1] Deinstalling postgresql16-client-16.4... [1/1] Deleting files for postgresql16-client-16.4: 100%

Install PostgreSQL 17 Package

sudo pkg install postgresql17-server

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 2 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        postgresql17-client: 17.0
        postgresql17-server: 17.0

Number of packages to be installed: 2

The process will require 69 MiB more space.
20 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/2] Fetching postgresql17-server-17.0.pkg: 100%   17 MiB  17.4MB/s    00:01
[2/2] Fetching postgresql17-client-17.0.pkg: 100%    4 MiB   3.7MB/s    00:01
Checking integrity... done (0 conflicting)
[1/2] Installing postgresql17-client-17.0...
[1/2] Extracting postgresql17-client-17.0: 100%
[2/2] Installing postgresql17-server-17.0...
===> 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
  ===
[2/2] Extracting postgresql17-server-17.0: 100%
=====
Message from postgresql17-client-17.0:

--
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 postgresql17-server-17.0:

--
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
~postgres/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_login_class="postgres"' to /etc/rc.conf, or
set it as the postgres user's login class in /etc/passwd.

========

To use PostgreSQL, enable it in rc.conf using

  sysrc postgresql_enable=yes

To initialize the database, run

  service postgresql initdb

You can then start PostgreSQL by running:

  service postgresql start

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

NB. FreeBSD's PostgreSQL port logs to syslog by default
    See ~postgres/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 be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
    using pg_checksums.  Check the initdb(1) manpage for more info
    and make sure you understand the performance implications.
sudo service 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 this locale configuration:
  locale provider:   libc
  LC_COLLATE:  C
  LC_CTYPE:    C.UTF-8
  LC_MESSAGES: C.UTF-8
  LC_MONETARY: C.UTF-8
  LC_NUMERIC:  C.UTF-8
  LC_TIME:     C.UTF-8
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data17 ... 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
initdb: hint: 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/data17 -l logfile start
sudo su - postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data17 -l logfile start

waiting for server to start.... done
server started

Load Data to PostgreSQL 17

psql create user abc password 'abc'; psql -U postgres -f /usr/local/www/dbname_20241006.sql > /usr/local/www/dbname_20241006_load.txt

Restart Server

sudo shutdown -r now

Published: Oct. 6, 2024

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.