How to Install & Configure Postgres 14 on FreeBSD 13

Postgresql is an open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance.

It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. PostgreSQL is used as the primary data store or data warehouse for many web, mobile, geospatial, and analytics applications. PostgreSQL can store structured and unstructured data in a single product.

In this guide we are going to install Postgresql 14 in FreeBSD 13.

Prerequisites

To follow along, ensure you have the following:

  1. Updated FreeBSD 13 server
  2. Root access to the server
  3. Internet access from the server
  4. Basic knowledge of Linux terminal

Table of contents

  1. Ensure the server is up to date
  2. Installing and starting Postgres Server
  3. PostgreSQL Roles and Databases Authentication Methods
  4. Connecting to postgres database
  5. Configuring postgres 14 instance for remote access
  6. User management in Postgres 14

1. Ensure the server is up to date

Before proceeding, let us ensure that our server has up to date packages. First, update the repo package database

pkg update

Then use this command to update the packages:

pkg upgrade

Let us also install vim we will need to update the config later

pkg install -y vim

2. Installing and starting Postgres Server

Use this command

pkg install postgresql14-server postgresql14-contrib

Enable Postgresql in rc.conf by running the following command.

sysrc postgresql_enable="YES"

Now you can start the Postres server by running the following command.

Once the installation is complete, initialize the PostgreSQL database with the following command:

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

Start the postgres using this command

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

Confirm status

# /usr/local/etc/rc.d/postgresql status
pg_ctl: server is running (PID: 16278)
/usr/local/bin/postgres "-D" "/var/db/postgres/data14"

Now enable service on boot

# service postgresql enable
postgresql enabled in /etc/rc.conf

Next, let us verify that the installation was successful by connecting to the PostgreSQL database server and printing its version:

su -m postgres -c 'psql -c "SELECT version();"'

Output:

# su -m postgres -c 'psql -c "SELECT version();"'
                                                                               version
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.0 on amd64-portbld-freebsd13.0, compiled by FreeBSD clang version 11.0.1 ([email protected]:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe), 64-bit
(1 row)

3. PostgreSQL Roles and Databases Authentication Methods

PostgreSQL uses a concept called roles to handle client authentication and authorization. By default, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

The installation procedure created a user account called¬†postgres that is associated with the default¬†postgres¬†role. In order to use PostgreSQL, you can log in to that account.

PostgreSQL supports multiple authentication methods . The most commonly-used methods are:

  • Trust¬†‚Äď A role can connect without a password, as long as the conditions defined in the¬†pg_hba.conf¬†are met.
  • Password¬†‚Äď A role can connect by providing a password. The passwords can be stored as¬†scram-sha-256,¬†md5, and¬†password¬†(clear-text).
  • Ident¬†‚Äď Only supported on TCP/IP connections. It works by obtaining the client‚Äôs operating system user name, with an optional user name mapping.
  • Peer¬†‚Äď Same as Ident, but it is supported on local connections only.

4. Connecting to postgres database

  1. By Switching to postres user

Switch over to the postgres account on your server by typing this in the terminal;:

su -u postgres

You can now access a Postgres prompt immediately by typing:

$ psql
psql (14.0)
Type "help" for help.

postgres=#

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

  1. By running the command as postgres user

Use this to run the command directly as the postgres user using sudo

<meta charset="utf-8"><meta charset="utf-8">su -m postgres -c psql

Output:

 # su -m postgres -c psql
psql (14.0)
Type "help" for help.

postgres=#

5. Configuring postgres 14 instance for remote access

To achieve this, we will modify postgres configuration files. We need to open the files and adjust the configs are required. The main configuration file for Postgresql 14 can be found in this path /var/db/postgres/data14/pg_hba.conf

Let’s change peer identification to trust:

sed -i '/^local/s/peer/trust/' /var/db/postgres/data14/pg_hba.conf

Change ident identification to md5 to allow password login.

sed -i '/^host/s/ident/md5/' /var/db/postgres/data14/pg_hba.conf

Add a block to allow access from everywhere:

Add this content to the file /var/db/postgres/data14/pg_hba.conf

host    all             all             0.0.0.0/0                md5

Ensure PostgreSQL is listening on *

Add this line to the config here /var/db/postgres/data14/postgresql.conf

listen_addresses='*'

Enable and restart postgresql server to reload the configs

service postgresql restart

6. User management

Creating Superuser

Now that everything is set up, let us create a super user.
Connect to the DB as postres role:

<meta charset="utf-8"># su -m postgres -c psql
psql (14.0)
Type "help" for help.

postgres=#

Create super user with name root:

CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';

Output:

postgres=# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root      | Superuser, Create role, Create DB                          | {}

postgres=#

Managing Application Users

Use this to create a database, create a user and grant that user all accesss to that database:

create database app_db_name;
create user app_user with encrypted password 'dbpassword';
grant all privileges on database app_db_name to app_user;

Checkout this comprehensive guide on user and permission management in postgres here.

Connecting to the instance from remote host

Use this command to connect to the postgres instance from local machine:

psql 'postgres://<username>:<password>@<host>:<port>/<db>?sslmode=disable'

# like
psql 'postgres://root:[email protected]:5432/postgres?sslmode=disable'

Conclusion

Up to this point we have managed to install Postgresql 14 on an FreeBSD 13 server, do some basic configurations then do basic user management.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy