I wanted to access PostgreSQL database running on FreeBSD over the LAN from macOS, and thought I would log this setup for reference, covering:
- Installation of PostgreSQL on FreeBSD
- Changing the password for the default postgres user
- Installation of pgAdmin4 on FreeBSD
- Allowing remote connections to PostgreSQL
- Connecting remotely to PostgreSQL from macOS
For completeness, the following versions are referred to:
PostgreSQL | 11.5 |
pgAdmin4 | 4.15 |
FreeBSD | 12.0-RELEASE-p9 |
macOS Sierra | 10.12.6 |
Installation of PostgreSQL on FreeBSD
Running pkg search 'PostgreSQL' | grep 'most advanced'
will show what versions of PostgreSQL can be installed on FreeBSD. For me this provides the following output:
postgresql10-server-10.10 PostgreSQL is the most advanced open-source database available anywhere
postgresql11-server-11.5_1 PostgreSQL is the most advanced open-source database available anywhere
postgresql12-server-12.r1 PostgreSQL is the most advanced open-source database available anywhere
postgresql94-server-9.4.24 PostgreSQL is the most advanced open-source database available anywhere
postgresql95-server-9.5.19 PostgreSQL is the most advanced open-source database available anywhere
postgresql96-server-9.6.15 PostgreSQL is the most advanced open-source database available anywhere
In this case I went for 11.5 and installed by running the following as root:
pkg install postgresql11-server-11.5_1
to install/usr/local/etc/rc.d/postgresql initdb
to initialise the dbecho 'postgresql_enable="YES"' >> /etc/rc.conf
to start on startup/usr/local/etc/rc.d/postgresql start
to start the daemon
Changing the password for the default postgres user
On installation, PostgreSQL creates one user with no password, called postgres. We should change this:
-
Run
psql postgres postgres
and you will be greeted with the following promptpsql (11.5) Type "help" for help. postgres=#
-
To change your password run
\password postgres
and put in the new one twice
Installation of pgAdmin4 on FreeBSD
virtualenv-3.6 pgadmin4
to setup a new virtualenvsource pgadmin4/bin/activate
to activate our vitualenvpip install pyopenssl cryptography pyasn1 ndg-httpsclient
will install some dependenciessudo pkg install py36-sqlite3
the binding for sqllite3, installed to the system- To install pgAdmin4, first one should check to see what the latest pip-related release is at https://ftp.postgresql.org/pub/pgadmin/pgadmin4/ . Once a URL for the latest version for pip has been found this can be passed to pip with
pip install 'https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.15/pip/pgadmin4-4.15-py2.py3-none-any.whl'
and it will install, along with further dependencies - The example config file can be copied with
cp ./pgadmin4/lib/python3.6/site-packages/pgadmin4/config.py ./pgadmin4/lib/python3.6/site-packages/pgadmin4/config_local.py
- The example config should then be edited to have it bind to an IP address you can access outside of localhost with
sudo vim ./pgadmin4/lib/python3.6/site-packages/pgadmin4/config_local.py
or similar
To do this we change DEFAULT_SERVER = ’127.0.0.1’ to DEFAULT_SERVER = ’0.0.0.0’
Clearly, one should not do this on a machine with a publicly exposed IP address — the assumption here is that the environment is a safely firewalled LAN sudo python ./pgadmin4/lib/python3.6/site-packages/pgadmin4/pgAdmin4.py
will start pdAdmin4. Having put in an email and password to login with, the web interface is athttp://192.168.x.x:5050/
where 192.168.x.x is the IP of your server- If you are running pgAdmin4 on the same server as PostgreSQL, you can simply fill out localhost as the host, postgres as the user, and whatever the postgres password was changed to
Allowing remote connections to PostgreSQL
If we run e.g. pgcli -h 192.168.x.x
(replace x.x as appropriate) we are greeted with the following error message:
could not connect to server: Connection refused
Is the server running on host "192.168.x.x" and accepting
TCP/IP connections on port 5432?
This is because by default PostgreSQL is not set up to allow remote connections. To allow this:
- Subnets that are allowed to send requests can be added to pg_hba.conf with
sudo vim /var/db/postgres/data11/pg_hba.conf
I addedhost all all 192.168.0.0/16 trust
but you may want to go narrower on the subnet.
https://www.dan.me.uk/ipsubnets is useful for looking up the CIDR notation.
Again I would only suggest doing this on a completely locked down LAN. - We also need to have PostgreSQL bind to an IP which is not the local loopback. To do this we can
sudo vim /var/db/postgres/data11/postgresql.conf
and change listen_addresses = ‘localhost’ to listen_addresses = ’*’ sudo /usr/local/etc/rc.d/postgresql restart
to restart
Connecting remotely to PostgreSQL from macOS
-
On macOS, I wanted a quick PostgreSQL client so we can check our server is up. pgcli is ideal for this, and is installed conveniently with
brew install pgcli
-
The connection can be tested with
pgcli -u postgres -h 192.168.x.x -d postgres
(replace x.x as appropriate). This should yield the below prompt or similar:Server: PostgreSQL 11.5 Version: 2.1.1 Chat: https://gitter.im/dbcli/pgcli Mail: https://groups.google.com/forum/#!forum/pgcli Home: http://pgcli.com postgres@192:postgres>