ETLMCPData Conveyance Works
Log in Start free
Guides

Open a self-hosted PostgreSQL server to etlmcp: listen_addresses, SSL and pg_hba.conf

The three settings to change on a PostgreSQL server you run yourself, on a VPS or bare metal, before etlmcp's Test can reach it over SSL.

A fresh PostgreSQL install on your own server refuses etlmcp three times over: it listens only on the loopback interface, it has SSL turned off, and its pg_hba.conf has no line that admits a remote client. etlmcp connects with sslmode=require by default, so it does not fall back to an unencrypted session. Change listen_addresses, turn on ssl, add a hostssl line, open the port to etlmcp's address alone, and the Test button on the destinations page can reach the server.

1. Listen on a public interface

The listen_addresses setting in postgresql.conf defaults to localhost. With that value the server takes TCP connections only from the machine itself, so a connection from anywhere else never reaches PostgreSQL at all. Set it to the server's public address, or to * for every interface, and rely on the firewall and pg_hba.conf to narrow who gets in.

This setting is read only when the server starts. A reload does nothing for it: restart PostgreSQL after you change it. The port, 5432 by default, is also a start-time setting.

2. Turn on SSL

The ssl setting is off by default. Set ssl = on in postgresql.conf. The server then needs a certificate and a private key. By default it looks for server.crt and server.key in the data directory; ssl_cert_file and ssl_key_file point elsewhere if your certificates live in another folder, for example where a certificate tool renews them.

PostgreSQL rejects a key file that group members or other users can read. Give it mode 0600, owned by the database user. The documented alternative is a key owned by root with group read access (0640), where the PostgreSQL user belongs to that group. If a file is wrong when the server starts, it will not start; if it is wrong on a reload, the old SSL setup stays in place and the error goes to the log. Since you restart for listen_addresses anyway, make both changes before that one restart.

3. Add a hostssl line to pg_hba.conf

Each line in pg_hba.conf lists a connection type, a database, a user, a client address and an authentication method. The first line that matches all of them decides the connection, and if none matches, PostgreSQL denies it. A hostssl line matches only connections that use SSL, which is what etlmcp sends.

Write one line for the database and user you created for etlmcp, from etlmcp's single address, with SCRAM passwords:

# TYPE   DATABASE  USER    ADDRESS          METHOD
hostssl  analytics etlmcp  203.0.113.7/32   scram-sha-256

Replace the address with the one etlmcp shows (see step 4). A /32 mask matches exactly one IPv4 address; an IPv6 address takes /128. Put the line above any broader rule, since the first match wins and there is no fall-through.

pg_hba.conf needs no restart. PostgreSQL re-reads it on a reload: run pg_ctl reload with the data directory, or run SELECT pg_reload_conf(); from a SQL session. The pg_hba_file_rules view shows any line it could not parse.

4. Open the firewall to etlmcp only

When etlmcp is set up with a fixed outgoing address, the destinations page names it under the destination list, with a line telling you to allow connections from it if you sit behind a firewall. Open the PostgreSQL port in your host firewall, or your VPS provider's firewall, for that address alone.

5. Use a public host name

etlmcp resolves the host you enter and refuses it if any address it resolves to is private, loopback, link-local, reserved, multicast or in the shared range 100.64.0.0/10. The error is host not allowed. A host name that resolves to a LAN or VPN address will fail this way, even if your own machine reaches it; enter a name or address that points to the server's public IP.

6. Press Test

Testing a PostgreSQL destination inserts one sample row and rolls it back, so nothing stays in your database. If it fails, the PostgreSQL destination errors page explains each message. For what require does and does not check about the server's certificate, see PostgreSQL SSL modes; for the grants the etlmcp user needs, see PostgreSQL privileges.

Related

Sources

  1. Connections and Authentication — www.postgresql.org, checked
  2. Secure TCP/IP Connections with SSL — www.postgresql.org, checked
  3. The pg_hba.conf File — www.postgresql.org, checked