PostgreSQL 12 on OpenBSD 6.7: Install

Table of contents

OpenBSD (opens in a new tab) 6.7 was released (opens in a new tab) last month and PostgreSQL (opens in a new tab) was upgraded from 11.7 to 12.2 then. The installation process of PostgreSQL server in OBSD 6.7 is almost the same to that in 6.5.

Environment Environment

  • OS: OpenBSD 6.7
  • DB: PostgreSQL 12.2

Summary Summary

The overall steps are below:

console
$ doas pkg_add postgresql-server

$ doas su _postgresql
$ cd /var/postgresql/
$ # changed: `--auth=md5` -> `--auth=scram-sha-256`
$ initdb -D /var/postgresql/data/ -U postgres --auth=scram-sha-256 --pwprompt --encoding=UTF-8 --locale=xx_XX.UTF-8
$ # password of superuser = postgres is asked
$ exit

$ doas rcctl enable postgresql
$ doas rcctl start postgresql

Each of them will be described at the rest of this post as tutorial.

Tutorial Tutorial

Install Install

Get the application package from ports (opens in a new tab) system:

console
$ doas pkg_add postgresql-server

Init database Init database

Switch user to _postgresql which was created at the package installation above in order to avoid error on permission:

console
$ doas su _postgresql
$ cd /var/postgresql/

Run init_db (opens in a new tab) to create a database cluster:

console
$ initdb -D /var/postgresql/data/ -U postgres --auth=scram-sha-256 --pwprompt --encoding=UTF-8 --locale=xx_XX.UTF-8

Here, --auth=scram-sha-256 and --pwprompt are for the sake of security. --auth=scram-sha-256 can be relaced by --auth=md5. --locale is up to your environment. In my case, it’s ja_JP.UTF-8. In order not to specify locale, run without --encoding=UTF-8 --locale=xx_XX.UTF-8 instead:

diff
- --encoding=UTF-8 --locale=xx_XX.UTF-8
+ --no-locale

The password of the superuser aka postgres is asked:

console
Enter new superuser password: 
Enter it again: 

After the process is completed, exit from _postgersql user:

console
$ exit

Start PostgreSQL server Start PostgreSQL server

Activate the daemon and start it:

console
$ doas rcctl enable postgresql
$ doas rcctl start postgresql
postgresql(ok)

Finished.

After Installation After Installation

The config files such as postgresql.conf and pg_hba.conf were created. They are useful to configure your servers (opens in a new tab).

psql (opens in a new tab) was also installed. It is used as a terminal-based front-end to PostgreSQL. By using the password asked above, it’s able to connect to the server:

console
$ psql -U postgres

Thank you for your reading :)

More on: Run a database on your server

OBSD Solutions

OBSD Solutions: OpenBSD consulting and support by Scqr, Reflerd Inc. (opens in a new tab)

Comments and feedback are welcome.