Install PostgreSQL on FreeBSD 13

This article will take you through how to install PostgreSQL on FreeBSD 13. PostgreSQL is a relational database management system (RDBMS) that emphasizes flexibility and SQL compliance. PostgreSQL supports both SQL relational querying and JSON non-relational querying. Among the features provided by PostgreSQL are triggers, foreign keys, materialized views, ACID-compliant transactions, and stored procedures.

How to Install PostgreSQL on FreeBSD 13

  • Update your system.
sudo pkg update
  • Next, install PostgreSQL using the following command.
sudo pkg install postgresql14-server postgresql14-contrib

Sample output

Checking integrity... done (0 conflicting)
[1/3] Installing postgresql14-client-14.4...
[1/3] Extracting postgresql14-client-14.4: 100%
[2/3] Installing postgresql14-server-14.4...
===> Creating groups.
Creating group 'postgres' with gid '770'.
===> Creating users
Creating user 'postgres' with uid '770'.
===> 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/3] Extracting postgresql14-server-14.4: 100%
[3/3] Installing postgresql14-contrib-14.4...
[3/3] Extracting postgresql14-contrib-14.4: 100%
=====
Message from postgresql14-client-14.4:

--
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 postgresql14-server-14.4:

--
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_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
The PostgreSQL contrib utilities have been installed. Please see
/usr/local/share/doc/postgresql/contrib/README
for more information./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
    pg_checksums.  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
=====
Message from postgresql14-contrib-14.4:

--
The PostgreSQL contrib utilities have been installed. Please see
/usr/local/share/doc/postgresql/contrib/README
for more information.
  • After the installation, enable it by running the command below.
sysrc postgresql_enable="YES"
  • Then initialize it.
/usr/local/etc/rc.d/postgresql initdb
  • Next, start PostgreSQL using the following command.
/usr/local/etc/rc.d/postgresql start
  • Then enable PostgreSQL to start on system boot.
service postgresql enable
  • To check the status, use the command below.
/usr/local/etc/rc.d/postgresql status

Connect to PostgreSQL on FreeBSD 13

  • Run the command below to connect to PostgreSQL.
sudo -u postgres psql
  • Then set the password for PostgreSQL by running the following command.
\password postgres

Create PostgreSQL database

  • Create a user with a password using the command below.
CREATE USER itnixpro WITH PASSWORD 'YourStrongPassword';
  • Then create a database.
CREATE DATABASE nixdb OWNER itnixpro;
  • To view your PostgreSQL databases, use the following command.
\l
  • Quit the PostgreSQL shell.
\q

Sample output

psql (14.4)
Type "help" for help.

postgres=# \password postgres
Enter new password for user "postgres": 
Enter it again: 
postgres=#  CREATE USER itnixpro WITH PASSWORD 'YourStrongPassword';
CREATE ROLE
postgres=# CREATE DATABASE nixdb OWNER itnixpro;
CREATE DATABASE
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+----------+----------+---------+---------+-----------------------
 nixdb     | itnixpro | UTF8     | C       | C.UTF-8 | 
 postgres  | postgres | UTF8     | C       | C.UTF-8 | 
 template0 | postgres | UTF8     | C       | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)

postgres=# \q
  • You have made it to the end of our article. We have gone through how to install PostgreSQL on FreeBSD 13.

Read more on PostgreSQL Documentation

Other Tutorials

Install PostgreSQL on Fedora 36

Install PostgreSQL on Ubuntu 22.04

Install PHP 8 on OpenSUSE

System administrator | Software Developer | DevOps

Leave a Comment