My Database Friend PostgreSQL

Next: Count all rows for all tables in a schema in PostgreSQL 9.3 UP: TOC

This is a straight forward guide for people who want to safely upgrade PostgreSQL Version 9.5 to 9.6.

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 php70-pdo_pgsql-7.0.14 The pdo_pgsql shared extension for php php70-pgsql-7.0.14 The pgsql shared extension for php postgresql95-client-9.5.5_1 PostgreSQL database (client) postgresql95-server-9.5.5_1 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" 9.5.5

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=pgsql \ --file=/usr/local/www/wnk20170106.sql

Check size of the exported data and ddl

ls -altr /usr/local/www/*.sql -rw-r--r-- 1 root wheel 1370834115 Jan 6 12:56 /usr/local/www/wnk20170106.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/

Change the following values

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

Stop PostgreSQL 9.5 service and delete binaries

service postgresql stop pkg delete -fy postgresql95-client-9.5.5_1 Updating database digests format: 100% 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: postgresql95-client-9.5.5_1 Number of packages to be removed: 1 The operation will free 10 MiB. [1/1] Deinstalling postgresql95-client-9.5.5_1... [1/1] Deleting files for postgresql95-client-9.5.5_1: 100% pkg delete -fy postgresql95-server-9.5.5_1 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: postgresql95-server-9.5.5_1 Number of packages to be removed: 1 The operation will free 18 MiB. [1/1] Deinstalling postgresql95-server-9.5.5_1... [1/1] Deleting files for postgresql95-server-9.5.5_1: 100% ==> You should manually remove the "pgsql" user. ==> You should manually remove the "pgsql" group

We save to old environment data

mv /usr/local/pgsql /usr/local/pgsql.old; ls -altr /usr/local/pgsql.old total 28 -rw------- 1 pgsql pgsql 179 Dec 11 11:05 pg_upgrade_server.log -rw------- 1 pgsql pgsql 179 Dec 11 11:05 pg_upgrade_utility.log -rw------- 1 pgsql pgsql 326 Dec 11 11:05 pg_upgrade_internal.log drwxr-xr-x 3 pgsql pgsql 512 Dec 11 11:05 . -rw------- 1 pgsql pgsql 246 Dec 11 11:18 .psql_history drwx------ 19 pgsql pgsql 1024 Dec 11 11:38 data drwxr-xr-x 17 root wheel 512 Dec 11 11:39 ..

Install PostgreSQL 9.6

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

portsnap fetch update cd /usr/ports/databases/postgresql96-server/ && make install clean Installing postgresql96-server-9.6.1_1... ===> Creating groups. Creating group 'postgres' with gid '770'. ===> Creating users Creating user 'postgres' with uid '770'. =========== BACKUP YOUR DATA! ============= As always, backup your data before upgrading. If the upgrade leads to a higher minor revision (e.g. 8.3.x -> 8.4), a dump and restore of all databases is required. This is *NOT* done by the port! =========================================== 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 ====================================================================== To run PostgreSQL at startup, add 'postgresql_enable="YES"' to /etc/rc.conf ===> SECURITY REPORT: This port has installed the following files which may act as network servers and may therefore pose a remote security risk to the system. /usr/local/bin/postgres This port has installed the following startup scripts which may cause these network services to be started at boot time. /usr/local/etc/rc.d/postgresql If there are vulnerabilities in these programs there may be a security risk to the system. FreeBSD makes no guarantee about the security of ports included in the Ports Collection. Please type 'make deinstall' to deinstall the port if this is a concern. For more information, and contact details about the security status of this software, see the following webpage: ===> Cleaning for postgresql96-client-9.6.1 ===> Cleaning for postgresql96-server-9.6.1_1

Next we create an initial database

/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/data96 ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok 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/data96 -l logfile start

Let's start the database

service postgresql start LOG: ending log output to stderr HINT: Future log output will go to log destination "syslog".

Restore the exported databases

su postgres psql -f /usr/local/www/wnk20170106b.sql > /var/log/wnk20170106.log psql -l dbwnk2016

Reboot Virtual Maching

shutdown -r now


After successful upgrade you should delete the snapshot of the virtual machine and delete the files in /usr/local/pgsql.old.

Next: Count all rows for all tables in a schema in PostgreSQL 9.3 UP: TOC