Installing a PostgreSQL Server under NetBSD is pretty easy. The recommended way is to install the prebuilt postgresql binaries. The PostgreSQL Server depends on the postgreSQL Client. Contents

  1. Setting PKG_PATH
  2. Installing the PostgreSQL Server
  3. Configuring the Server
  4. Starting the Server
  5. Start on every boot
  6. Creating an example Database
  7. Using the Database
  8. PHP and PostgreSQL
  9. Additional Information

Setting PKG_PATH

Setting PKG_PATH enables us to easily download and install packages and it's dependencies.

# export PKG_PATH=$(uname -m)/$(uname -r)/All/ 

or if you are using csh:

# setenv PKG_PATH$(uname -m)/$(uname -r)/All/ 

You should keep this in your .profile, .bash_profile, if you mainly using binaries.

Installing the PostgreSQL Server

# pkg_add -v postgresql92-server

This will install the postgresql client and the server and any missing dependency (readline, perl) and will add the user pgsql to your user database.

That's it. Almost.

Configuring the Server

Please copy the pgsql example script to /etc/rc.d/

# cp /usr/pkg/share/examples/rc.d/pgsql /etc/rc.d

If you want to keep the database cluster other place than the default location, just change the user pgsql's home directory, before proceeding to the initialisation:

# usermod -d /var/db/pgsql pgsql

This directory must be owned by pgsql:pgsql.

and then

Starting the Server

If you want to initialise the database with a local other than "C", for example with unicode, invoke this before starting postgresql for the first time:

# /etc/rc.d/pgsql initdb -E unicode

please start the server entering:

# /etc/rc.d/pgsql start

This will create all necessary initial databases on the first start.

Start on every boot

To start the server on every boot add


to your /etc/rc.conf

Creating an example Database

To create a database switch to the user pgsql

# createdb -e -h -U pgsql newdbname

and create the database

$ createdb testdb
$ exit

Using the Database

Postgres provides a Tool to manage the Database called psql.

# psql -U pgsql testdb
Welcome to psql 8.0.4, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit


PHP and PostgreSQL

You may wish to install the postgres Module for PHP, e.g. for PHP 7.0 use:

# pkg_add -v php70-pgsql

Have fun.

Additional Information