avatar

Andres Jaimes

How to install postgres on FreeBSD

By Andres Jaimes

- 5 minutes read - 853 words

PostgreSQL, also known as Postgres, is a popular open-source relational database management system (RDBMS) that is widely used by developers and organizations around the world. FreeBSD, a free and open-source operating system, is well-known for its reliability, performance, and security features. In this blog post, we will guide you through the process of installing PostgreSQL in FreeBSD. Whether you’re a developer or a system administrator, this step-by-step tutorial will help you get started with it.

Installing Postgres

You have to be a local administrator to use pkg, the FreeBSD package manager. Switch to root to start the process:

su -

Search and install postgres. I recommend using the latest version.

pkg search postgresql
pkg install postgresql15-server
=====
Message from postgresql15-server-15.1_1:

--
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
~postgres/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 ~postgres/data/postgresql.conf

NB. FreeBSD's PostgreSQL port logs to syslog by default
    See ~postgres/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 be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
    pg_checksums.  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

Configure postgres to autostart by adding postgresql_enable="YES" to /etc/rc.conf:

vi /etc/rc.conf
...
postgresql_enable="YES"
...

Initialize the database:

/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 this locale configuration:
  provider:    libc
  LC_COLLATE:  C
  LC_CTYPE:    C.UTF-8
  LC_MESSAGES: C.UTF-8
  LC_MONETARY: C.UTF-8
  LC_NUMERIC:  C.UTF-8
  LC_TIME:     C.UTF-8
The default text search configuration will be set to "english".

Data page checksums are disabled.

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

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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/data15 -l logfile start

And start it:

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

You can verify it works by connecting to it using the default user, conveniently called postgres:

psql -U postgres
psql (15.1)
Type "help" for help.

postgres=#

The postgres=# shell indicates that the process was successful. Press Ctrl-D to return to the regular shell.

Create a database and a user

Connect to postgres, like we did before:

psql -U postgres

and enter the following sql:

CREATE DATABASE mydatabase;
CREATE USER myusername WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myusername;

Connecting from java

Download the postgres driver or add the dependency to your maven project:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version>
</dependency>

Then, a simple jdbc connection can be created like this:

static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/mydatabase";
static final String USER = "myusername";
static final String PASS = "mypassword";

Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);

// use the connection...

Conclusion

Installing PostgreSQL on FreeBSD is a relatively simple process that can be accomplished with just a few steps. By following the instructions outlined in this post, you can easily set up a Postgres database on your FreeBSD system, and start using it for your applications or projects. With its robust features and excellent performance, Postgres is an excellent choice for anyone looking for a powerful, open-source RDBMS. So, if you’re using FreeBSD, don’t hesitate to give PostgreSQL a try!