PostgreSQL 10.5 を FreeBSD 12.0-RELEASE に入れる2019年10月31日 13時18分44秒

FreeBSD で別のデータベースを試したく、PostgreSQL を入れてみた。FreeBSD 12.1-RELEASE が間近で、12.1-RELEASE 用の pkg を見るとこれよりも新しいバージョンもあるが、基本的な機能の実験が目的なので、少し古いものをインストールした。

まずは、pkg で確認。

# pkg search '^postgresql.*server'
postgresql10-server-10.5       PostgreSQL is the most advanced open-source datab
ase available anywhere
postgresql93-server-9.3.24     PostgreSQL is the most advanced open-source datab
ase available anywhere
postgresql94-server-9.4.19     PostgreSQL is the most advanced open-source datab
ase available anywhere
postgresql95-server-9.5.14     PostgreSQL is the most advanced open-source datab
ase available anywhere
postgresql96-server-9.6.10     PostgreSQL is the most advanced open-source datab
ase available anywhere
この中では最新を選んで pkg install。
# pkg install postgresql10-server
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking integrity... done (0 conflicting)
The following 2 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        postgresql10-server: 10.5
        postgresql10-client: 10.5

Number of packages to be installed: 2

The process will require 31 MiB more space.

Proceed with this action? [y/N]:
y
[1/2] Installing postgresql10-client-10.5...
[1/2] Extracting postgresql10-client-10.5: 100%
[2/2] Installing postgresql10-server-10.5...
===> Creating groups.
Creating group 'postgres' with gid '770'.
===> Creating users
Creating user 'postgres' with uid '770'.

  =========== BACKUP YOUR DATA! =============
  As always, backup your data before
  upgrading. If the upgrade leads to a higher
  minor revision (e.g. 8.3.x -> 8.4), a dump
  and restore of all databases is
  required. This is *NOT* done by the port!
  ===========================================
[2/2] Extracting postgresql10-server-10.5: 100%
Message from postgresql10-client-10.5:

The PostgreSQL port has a collection of "side orders":
postgresql-docs
  For all of the html documentation

p5-Pg
  A perl5 API for client access to PostgreSQL databases.

postgresql-tcltk
  If you want tcl/tk client support.

postgresql-jdbc
  For Java JDBC support.

postgresql-odbc
  For client access from unix applications using ODBC as access
  method. Not needed to access unix PostgreSQL servers from Win32
  using ODBC. See below.

ruby-postgres, py-psycopg2
  For client access to PostgreSQL databases using the ruby & python
  languages.

postgresql-plperl, postgresql-pltcl & postgresql-plruby
  For using perl5, tcl & ruby as procedural languages.

postgresql-contrib
  Lots of contributed utilities, postgresql functions and
  datatypes. There you find pg_standby, pgcrypto and many other cool
  things.

etc...
Message from postgresql10-server-10.5:

For procedural languages and postgresql functions, please note that
you might have to update them when updating the server.

If you have many tables and many clients running, consider raising
kern.maxfiles using sysctl(8), or reconfigure your kernel
appropriately.

The port is set up to use autovacuum for new databases, but you might
also want to vacuum and perhaps backup your database regularly. There
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
you may find useful. You can use it to backup and perform vacuum on all
databases nightly. Per default, it performs `vacuum analyze'. See the
script for instructions. For autovacuum settings, please review
~pgsql/data/postgresql.conf.

If you plan to access your PostgreSQL server using ODBC, please
consider running the SQL script /usr/local/share/postgresql/odbc.sql
to get the functions required for ODBC compliance.

Please note that if you use the rc script,
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
(UTF-8) will be used to store character data by default.  Set
postgresql_initdb_flags or use login.conf settings described below to
alter this behaviour. See the start rc script for more info.

To set limits, environment stuff like locale and collation and other
things, you can set up a class in /etc/login.conf before initializing
the database. Add something similar to this to /etc/login.conf:
---
postgres:\
        :lang=en_US.UTF-8:\
        :setenv=LC_COLLATE=C:\
        :tc=default:
---
and run `cap_mkdb /etc/login.conf'.
Then add 'postgresql_class="postgres"' to /etc/rc.conf.

======================================================================

To initialize the database, run

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

You can then start PostgreSQL by running:

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

For postmaster settings, see ~pgsql/data/postgresql.conf

NB. FreeBSD's PostgreSQL port logs to syslog by default
    See ~pgsql/data/postgresql.conf for more info
NB. If you're not using a checksumming filesystem like ZFS, you might
    wish to enable data checksumming. It can only be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgres_initdb_flags rcvar.  Check the initdb(1) manpage
    for more info and make sure you understand the performance
    implications.

======================================================================

To run PostgreSQL at startup, add
'postgresql_enable="YES"' to /etc/rc.conf
沢山表示が出てきたが、素で動かすのに必要なのは、三つのコマンドのみ。しっかり読むと上に記載されている。

/etc/rc.conf を更新。

# echo 'postgresql_enable="YES"' >> /etc/rc.conf
データベースを初期化する。
# /usr/local/etc/rc.d/postgresql initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data10 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/bin/pg_ctl -D /var/db/postgres/data10 -l logfile start
データベースをサーバとして起動する。
# /usr/local/etc/rc.d/postgresql start
2019-09-24 05:42:03.884 EDT [10785] LOG:  listening on IPv6 address "::1", port
5432
2019-09-24 05:42:03.885 EDT [10785] LOG:  listening on IPv4 address "127.0.0.1",
 port 5432
2019-09-24 05:42:03.885 EDT [10785] LOG:  listening on Unix socket "/tmp/.s.PGSQ
L.5432"
2019-09-24 05:42:03.896 EDT [10785] LOG:  ending log output to stderr
2019-09-24 05:42:03.896 EDT [10785] HINT:  Future log output will go to log dest
ination "syslog".
この後は、パスワードの設定を行った方が良いが、今回は単体で今だけ少し使いたいので省略。後々調べてみたい。

psql コマンドで、postgres ユーザで接続する。

% psql -U postgres
psql (10.5)
Type "help" for help.
折角なので少しだけ実験。
postgres=# help
You are using psql, the command-line interface to PostgreSQL.
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
postgres=# CREATE TABLE IF NOT EXISTS integers ( number1 INTEGER, number2 INTEGER )
postgres-# INSERT INTO integers ( number2, number1 ) VALUES ( 98, 2 );

次回