For the complete documentation index, see llms.txt. This page is also available as Markdown.

35.2 PostgreSQL

PostgreSQL is a free object-relational database management system (Object-Relational Database Management System, ORDBMS). Its academic origins trace back to the Ingres project at the University of California, Berkeley, where Professor Michael Stonebraker initiated the successor project POSTGRES (meaning "post-Ingres", i.e., the post-Ingres era) in 1986, and Version 1 was released to external users in June 1989.

PostgreSQL uses the PostgreSQL License, which is similar to the BSD License and is likewise a permissive open-source license.

As a database system that strictly adheres to ACID (Atomicity, Consistency, Isolation, Durability) properties, PostgreSQL supports transaction processing, complex query optimization, and data integrity constraints.

Installing PostgreSQL

PostgreSQL provides multiple stable versions to choose from. This section uses PostgreSQL 18 as an example; the installation and configuration methods for other versions are similar.

Install using pkg:

# pkg install postgresql18-server

Or install using Ports:

# cd /usr/ports/databases/postgresql18-server/
# make install clean

View the configuration information after PostgreSQL installation:

# pkg info -D postgresql18-server

File Structure

The file structure of PostgreSQL is as follows.

/
├── etc/
   ├── login.conf  # Login configuration file
   ├── rc.conf     # System startup configuration
   └── passwd      # User password file
├── usr/
   ├── local/
      ├── etc/
         ├── rc.d/
            └── postgresql  # PostgreSQL startup script
         └── periodic/
             └── daily/
                 └── 502.pgsql  # PostgreSQL scheduled backup script
      ├── share/
         └── postgresql/
             └── odbc.sql  # ODBC compatibility SQL script
      └── bin/
          └── pg_ctl  # PostgreSQL control utility
   └── ports/
       └── databases/
           └── postgresql18-server/  # PostgreSQL 18 Port
└── var/
    └── db/
        └── postgres/
            ├── data18/  # PostgreSQL 18 data directory
               └── postgresql.conf  # PostgreSQL configuration file
            ├── main/    # main instance data directory
            └── dev/     # dev instance data directory

Service Management

Set the PostgreSQL service to start on boot:

Initializing the Database

A database cluster is a collection of directories where PostgreSQL stores data, containing all database files and configuration. The initialization method is as follows.

Initialize the PostgreSQL database cluster:

Example output (with internationalization settings; English output is also normal):

Logging In and Usage

PostgreSQL does not have a root user by default; you need to log in using the postgres user created during installation.

Example output:

Correct usage:

PostgreSQL Service Management

Initialization Recommendations

Initializing the database is recommended using the service postgresql initdb command, which is the most convenient method.

The following explains how to use the postgresql_initdb_flags rc script parameter.

The default value of the postgresql_initdb_flags parameter is "--encoding=utf-8 --lc-collate=C", which means: specifying UTF-8 encoding and C collation.

It is recommended to set it using sysrc as follows:

Set the PostgreSQL database initialization parameters: UTF-8 encoding, C collation, SCRAM-SHA-256 authentication (a secure authentication protocol based on the Salted Challenge Response Authentication Mechanism), and require a password to be entered.

Explanation:

  • -A scram-sha-256 specifies the default authentication method; otherwise, the default authentication method in pg_hba.conf is trust, which allows passwordless login;

  • -W requires setting the postgres user password during initialization, eliminating the need to set it after logging in.

This simplifies some initialization operations and also allows customizing initialization options such as encoding and authentication method through the postgresql_initdb_flags parameter.

Managing Multiple Database Instances (Clusters)

PostgreSQL can run multiple instances on a single machine, suitable for scenarios such as testing different configurations and environment isolation.

The PostgreSQL rc script encapsulates these management functions, making them equally convenient.

Below, two named instances, main and dev, will be created.

Configuring Instances

Notes:

  • ① Placing the instance name in the rc parameter name creates the corresponding instance parameter name.

  • ② Parameters such as postgresql_data and postgresql_enable can still be used. These parameters serve as default values for the corresponding instance parameters and are used when the specific instance parameter cannot be found. However, once instance parameters are explicitly configured, the default parameters are no longer used.

  • ③ When managing multiple instances, it is recommended to use only parameters with instance names. The advantage of this configuration is clarity and more flexible control, as different instances can have different settings.

Initializing Instance Directories

After configuration is complete, you can initialize the database instances.

Initialize the PostgreSQL database cluster:

This command initializes both database instances.

You can also specify the instance name at the end of the command to initialize them separately; this method is suitable for adding new instances.

Note

Two different instances must run on different ports, so the port number of the corresponding instance needs to be modified.

The main instance uses the default port 5432 and does not need modification; the dev instance uses port 5433, and its corresponding configuration file needs to be modified.

Edit the /var/db/postgres/dev/postgresql.conf file. Find the port line and modify it as follows:

Set the PostgreSQL listening port to 5433.

Starting, Stopping, and Restarting

After the instance configuration is complete, you can start or stop them.

The above operations apply to all instances. Example:

You can also specify an instance name to operate on a single instance:

Example output:

References

  • The PostgreSQL Global Development Group. A Brief History of PostgreSQL[EB/OL]. [2026-04-17]. https://www.postgresql.org/docs/current/history.html. Documents the complete history of the POSTGRES project from its Ingres successor to PostgreSQL, including key milestones such as the release of Version 1 in June 1989.

Last updated