User Tools

Site Tools


freebsdserver2016:postgres

PostgreSQL DB server

Running

pkg search postgres

shows a number of possible packages, most of which relate to accessing a postgres DB. It is the -server and -client packages that are of interest right now, several versions of those are available. Of course, I choose the most recent one, right now (2016-08-04) that's 9.5.3. To install, I run:

pkg install postgresql95-server-9.5.3 postgresql95-client-9.5.3

which first has me upgrading pkg, then it proposes to install:

        postgresql95-server: 9.5.3
        postgresql95-client: 9.5.3
        libxml2: 2.9.3
        gettext-runtime: 0.19.8.1
        indexinfo: 0.2.4
        perl5: 5.20.3_13

which I accept. Note, adding in jdbc makes for a much longer list of packages, so I'll wait with that until I really need it. The installer spits out the following notes:

[1/6] Installing indexinfo-0.2.4...
[1/6] Extracting indexinfo-0.2.4: 100%
[2/6] Installing gettext-runtime-0.19.8.1...
[2/6] Extracting gettext-runtime-0.19.8.1: 100%
[3/6] Installing perl5-5.20.3_13...
[3/6] Extracting perl5-5.20.3_13: 100%
[4/6] Installing postgresql95-client-9.5.3...
[4/6] Extracting postgresql95-client-9.5.3: 100%
[5/6] Installing libxml2-2.9.3...
[5/6] Extracting libxml2-2.9.3: 100%
[6/6] Installing postgresql95-server-9.5.3...
===> Creating groups.
Creating group 'pgsql' with gid '70'.
===> Creating users
Creating user 'pgsql' with uid '70'.

  =========== 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!

  Press ctrl-C *now* if you need to pg_dump.
  ===========================================

[6/6] Extracting postgresql95-server-9.5.3: 100%
Message from perl5-5.20.3_13:
The /usr/bin/perl symlink has been removed starting with Perl 5.20.
For shebangs, you should either use:

#!/usr/local/bin/perl

or

#!/usr/bin/env perl

The first one will only work if you have a /usr/local/bin/perl,
the second will work as long as perl is in PATH.
Message from postgresql95-client-9.5.3:
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-PyGreSQL
  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 postgresql95-server-9.5.3:
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 perfom vacuum on all
databases nightly. Per default, it perfoms `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

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

To run PostgreSQL at startup, add
'postgresql_enable="YES"' to /etc/rc.conf

Attempting to follow the advice on “/usr/local/etc/rc.d/postgresql initdb” results in the message:

Cannot 'initdb' postgresql. Set postgresql_enable to YES in /etc/rc.conf or use 'oneinitdb' instead of 'initdb'.

So, adding “postgresql_enable=“YES” to /etc/rc.conf results in the above command to succeed, generating some output:

# /usr/local/etc/rc.d/postgresql initdb
The files belonging to this database system will be owned by user "pgsql".
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 /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... 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 /usr/local/pgsql/data -l logfile start

And doing just that last thing as root fails:

# /usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

So:

# su -m pgsql -c "/usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start"
server starting
root@server2016:~ # cannot create logfile: Permission denied

It turns out the server isn't actually running. Retry with su - pgsql first:

# su - pgsql
$ /usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
server starting

Indeed the file /usr/local/pgsql/logfile has now been created, a postgres process is there.

Still in the shell as pgsql, create a database, then connect to it using psql:

createdb testdb
psql testdb

Type “help” for help, \q to quit.

A beginner's tutorial can be found e.g. here: http://www.tutorialspoint.com/postgresql/

The data types are not quite the same as for Oracle. number → numeric, varchar2 → varchar

After the upgrade to FreeBSD 11, postgresql wouldn't start, calling out a missing libssl.so.7. This I could finally fix by running another update:

pkg upgrade -f

after which “service postgresql start” worked out.

freebsdserver2016/postgres.txt · Last modified: 2017/02/03 18:20 by eric