Postgresql 9.x Streaming Repilication on FreeBSD – Part I

Installing Postgresql 9.3:

Installing and configuring Postgresql 9.3 for streaming replication is complex but not difficult.  Although replication can provide a certain level of redundancy it should never take the place of a good backup strategy.


Install Postgresql:

# portsnap fetch update
# cd /usr/ports/databases/postgresql93-server
# make install clean



add 'postgresql_enable="YES"' to /etc/rc.conf


Initialize the Database:

# /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
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/postgres -D /usr/local/pgsql/data
 /usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start



Configure PostgreSQL to listen for database connections on all system IP addresses by adding the following two lines to /usr/local/pgsql/data/postgresql.conf.

listen_addresses = ','
port = 5432

* note:  Comma-separated list of addresses is acceptable



Configure PostgreSQL to use password hash authentication for all hosts and users connecting from the local network by adding the following line to the /usr/local/pgsql/data/pg_hba.conf file. NOTE: Replace with your own network.

host    all             all             md5


Start Postgresql:

# service postgresql start


Add User:

Add new super user with database and role creation rights.

# /usr/local/etc/rc.d/postgresql start
# su pgsql
$ createuser -sdrP username
Enter password for new role: ******
Enter it again: ******
$ exit


Log Files:

To enable log files outside of /var/log/messages, several configuration changes are needed.


Edit /etc/syslog.conf:

Modify the following line – additions are in blue:

*.notice;authpriv.none;kern.debug;;mail.crit;news.err;local0.none               /var/log/messages


Add the following lines above the !*  (Bottom) line: 

*.*                                          /var/log/postgresql.log


Edit /etc/newsyslog.conf:

To rotate the log files at 1900 hrs and keep 7 days worth add the following line to /etc/newsyslog.conf.  Of course, modify to your environment as you wish.


/var/log/postgresql.log  pgsql:pgsql    640  7     *    @T19  GB    /usr/local/pgsql/data/ 30


Restart newsyslog service:

# service newsyslog restart


Create log file:

# cd /var/log
# touch postgresql.log
# chown pgsql:pgsql postgresql.log


By now, the postgresql server is fully configured and ready for use.  Reboot the server to ensure all the services restart and you can log in to the postgresql server with a tool such as PgAdmin.  Also, you may need to increase the minimum logging level to see activity in your log files.  As always, keep an eye on the log file to ensure the rotation is occurring as expected.  In Part II, configuring Postgresql with SSL is demonstrated.




Leave a Reply

Your email address will not be published. Required fields are marked *