> ## Content Index
> Fetch the complete content index at: https://ostreff.info/llms.txt
> Use this file to discover other available public pages before exploring further.

# Upgrade PostgreSQL 14.5 to 15.x on FreeBSD
- URL: https://ostreff.info/upgrade-postgresql-on-freebsd/
- Published: 2021-11-13T17:07:30.000Z
- Updated: 2022-10-14T10:29:59.000Z
- Author: Jordan Ostreff
- Tags: FreeBSD, PostgreSQL

New version of [postgresql 15 database](https://www.postgresql.org/about/featurematrix/?ref=ostreff.info) have arrived. I have updated the manual to newest possible upgraded version on 14 october 2022.

Stop your [postgresql](https://www.postgresql.org/?ref=ostreff.info) database daemon on [FreeBSD](https://freebsd.org/?ref=ostreff.info) machine:

```bash
service postgresql stop
```

Create package containing your old version ( 14.5 ) binaries ( depends are you using postgresql14-contrib package, but command will not fail if it is missing )

```bash
pkg create postgresql14-server postgresql14-contrib
```

Uncompress them somewhere in the filesystem - in our example it is into /tmp/pg-upgrade directory.

```bash
mkdir /tmp/pg-upgrade
tar xf postgresql13-server-14.5.pkg -C /tmp/pg-upgrade
tar xf postgresql13-contrib-14.5.pkg -C /tmp/pg-upgrade /* only needed if you have postgresql14-contrib package installed */
```

Remove old installed binaries

```bash
pkg delete -f databases/postgresql14-server databases/postgresql14-contrib databases/postgresql14-client
```

Install new version of PostgreSQL 15.x

```bash

portmaster databases/postgresql15-server databases/postgresql15-client
portmaster databases/postgresql15-contrib /* only if need it */
```

Create new empty database with

```bash
su -l postgres -c "/usr/local/bin/initdb --encoding=utf-8 --lc-collate=C -D /var/db/postgres/data15 -U postgres"
```

Migrate the data from old database to the new empty database. Assuming that previos database is leaving into /var/db/postgres/data14/ directory.

```bash
su -l postgres -c "pg_upgrade -b /tmp/pg-upgrade/usr/local/bin/ -d /var/db/postgres/data14/ -B /usr/local/bin/ -D /var/db/postgres/data15/ -U postgres"
```

Start your database

```bash
service postgresql start
```

Once you start the new server, consider running:

```bash
/usr/local/bin/vacuumdb -U postgres --all --analyze-in-stages
```

Running this script will delete the old cluster's data files:

```bash
/var/db/postgres/delete_old_cluster.sh
```

Some decrease of needed disk space is easy visible:

1,5G    data14
1,4G    data15

Source of the manual is comming from [FreeBSD Ports tree](https://cgit.freebsd.org/src/tree/UPDATING?h=stable/13&ref=ostreff.info).