Menu

How to Install PostgreSQL on FreeBSD: A Step-by-Step Tutorial

Install supported PostgreSQL 18 on FreeBSD with pkg, initialize its database cluster, and enable the service. PostgreSQL 13 is no longer supported.

Posted on By
On this page

PostgreSQL, often referred to as Postgres, is a robust and open-source relational database management system known for its advanced features and reliability. In this step-by-step tutorial, we will guide you through the process of installing PostgreSQL on a FreeBSD system. Additionally, we will cover essential database management tasks, providing you with the knowledge to work effectively with PostgreSQL on your FreeBSD-powered server.

Introduction

This guide installs PostgreSQL 18 from the FreeBSD ports packages postgresql18-server and postgresql18-contrib. PostgreSQL 18 is supported through November 2030; PostgreSQL 13 reached end of life in November 2025. See the PostgreSQL versioning policy and the FreeBSD PostgreSQL 18 server port.

Preconditions

Before you begin the installation process, ensure you have met the following prerequisites:

  1. A FreeBSD system with root or superuser access.
  2. A stable internet connection to download PostgreSQL packages.
  3. Familiarity with basic command-line operations in FreeBSD.

With these prerequisites in place, let’s proceed with the installation.

Installing PostgreSQL on FreeBSD

Step 1: Update the System

Start by ensuring your FreeBSD system is up to date. Open a terminal and run the following commands:

freebsd-update fetch install
pkg update

freebsd-update fetch install applies available FreeBSD base-system updates. pkg update refreshes the package catalog used to find PostgreSQL packages.

Step 2: Install PostgreSQL

Install the PostgreSQL 18 server and contrib packages:

pkg install postgresql18-server postgresql18-contrib

Check the installed client version:

psql --version

Step 3: Initialize the PostgreSQL Database

After the installation, initialize the PostgreSQL database cluster by running:

service postgresql initdb

This command will create the necessary directory structure and configuration files for PostgreSQL.

Step 4: Start and Enable PostgreSQL

To start the PostgreSQL service and enable it to start automatically at boot, use these commands:

service postgresql start
sysrc postgresql_enable=YES

Creating a PostgreSQL User and Database

Create a login role. createuser --pwprompt asks you to enter a password:

su - postgres
createuser --pwprompt your_user
createdb -O your_user your_database
exit

To illustrate PostgreSQL’s capabilities, let’s create a simple table in the newly created database. Access the PostgreSQL command-line tool, psql, using the following command:

su - postgres
psql -d your_database

Once in the psql prompt, execute the following SQL commands to create a basic table:

CREATE TABLE example (
    id serial PRIMARY KEY,
    name VARCHAR (100),
    age INT
);

Managing the PostgreSQL Service

To manage the PostgreSQL service on FreeBSD, you can use the following commands:

  • Start PostgreSQL service: service postgresql start

  • Stop PostgreSQL service: service postgresql stop

  • Restart PostgreSQL service: service postgresql restart

  • Check PostgreSQL service status: service postgresql status

Conclusion

Congratulations! You have successfully installed PostgreSQL on your FreeBSD system, created a database, and learned how to perform basic management tasks. PostgreSQL’s extensive feature set makes it a versatile choice for various data storage needs. To make the most of PostgreSQL, explore its documentation and tailor it to your specific requirements.

With PostgreSQL now installed and running, you are well-prepared to build and manage databases for your applications, leveraging its scalability and reliability for your data-driven projects.

If you want to learn more about PostgreSQL, please use our PostgreSQL tutorials and PostgreSQL Reference.