So you have installed Postgres onto your machine, and you want to start working with it.
How?
The key to understanding the post-installation procedure is to realize that it depends.
Let’s start by working with the most basic steps common to all installs, then break it down further from there.
A successfully installed Postgres instance, no matter the version, is characterized by the following:
There are other configuration files, but we will work with these.
For the purposes of discussion, let’s further assume you have started the cluster and Postgres is running on the host. Here is an example of what you can see when you run a utility, such as netstat, that reports network connections:
|
1 2 3 4 |
$ netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 27929/postgres |
The first thing to look at is the “Local Address”. Notice that it says 127.0.0.1:5432. This means the server is currently listening on localhost on port 5432. But you want 0.0.0.0:5432; otherwise, remote connections cannot be accepted. With an editor, open pg_hba.conf and look at the default rules. Keep in mind that the configuration file can be located in one of several locations. We will cover that later.
ATTENTION: Setting the address, or CIDR, to 0.0.0.0 is for connectivity purposes only. As soon as you know everything works, you should restrict this to as few permitted connections as possible. This is not something you should do on a production machine.
The actual rules per line can vary from one type of Postgres installation to another. The good news is that Red Hat/CentOS installs look similar, and Debian/Ubuntu installs have their own similar style. The relevant settings are at the bottom of the file, because everything above is commented documentation.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 peer # Allow replication connections from localhost, by a user with the # replication privilege. local replication all peer host replication all 127.0.0.1/32 md5 host replication all ::1/128 md5 |
Look at the first line, where TYPE is local. As long as you can log in locally via Unix domain sockets and use sudo as the superuser, postgres by default, you can access your service without a password.
METHOD should be peer. If it uses something else, such as md5, you will need to change the string. Alternatively, if you trust the other user accounts on the host, you can use the trust method, which permits free access to all locally logged-in Unix accounts.
|
1 2 3 4 5 6 7 8 |
# ATTENTION: # The service must be reloaded for edits to pg_hba.conf to take effect. $ sudo su - postgres $ psql -c "SELECT 'hello world' AS greetings;" greetings ------------- hello world |
Looking at the second line, you can see that TYPE is IPv4. This rule, as well as the rule for IPv6, prevents localhost logins unless one knows the password:
|
1 2 |
$ psql -h localhost -c "SELECT 'hello world' AS greetings;" Password for user postgres: |
Let’s fix this by assigning a password to the postgres role. Log in via Unix domain sockets, since we already permit logins using the peer method:
|
1 2 |
-- Example invocation. Change the password to something real. ALTER ROLE postgres WITH PASSWORD 'mypassword'; |
Tip: Edits to pg_hba.conf require the service to reload the file, also known as SIGHUP.
Now that we have connectivity for localhost connections, using an IPv4 socket for this example, we can proceed to address remote connections.
You need to add another rule, which should be placed after the localhost rule:
|
1 |
host all all 0.0.0.0/0 md5 |
Here is a line you can write for IPv6:
|
1 |
host all all ::0/0 md5 |
Tip: The example rules shown here let everybody connect to the host. Knowledge of CIDR is key to enforcing network security.
Keeping in mind that your system will be unique, here is what pg_hba.conf should start to look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 host all all ::0/0 md5 # Allow replication connections from localhost, by a user with the # replication privilege. local replication all peer host replication all 127.0.0.1/32 md5 host replication all ::1/128 md5 |
You are almost there.
Now that you have added a password to the superuser and updated the configuration file pg_hba.conf, it is time to visit another configuration file: postgresql.conf.
Locate the file and edit the runtime parameter listen_addresses. The default setting prohibits remote connections. Resetting the value either to a NIC’s IP address or to the wildcard makes it accessible.
Tip: As the postgres user, execute the following in a psql session to locate your configuration files.
|
1 |
SELECT DISTINCT sourcefile FROM pg_settings; |
For those feeling fancy, you can bind the Postgres service to more than one IP address as a comma-separated list:
|
1 2 3 4 5 |
listen_addresses = '*' # listen_addresses = 'localhost' # What IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all. # This change requires restart. |
An alternate method for updating runtime parameters can be accomplished using the SQL statement:
|
1 2 |
postgres=# ALTER SYSTEM SET listen_addresses = '*'; ALTER SYSTEM |
The final step, restarting the service, is where we start splitting hairs again:
Systemd:
|
1 |
/usr/pgsql-12/bin/postgresql-12-setup initdb |
|
1 2 |
systemctl start postgresql-12 systemctl stop postgresql-12 |
|
1 |
systemctl restart postgresql |
Debian-derived Linux distributions include a collection of command-line utilities to administer the PostgreSQL service:
|
1 2 3 |
# Example CLI. pg_ctlcluster Usage: /usr/bin/pg_ctlcluster [-- ] |
|
1 2 |
# Restarting Postgres version 12 on a Debian-derived distribution. pg_ctlcluster 12 main restart |
After a successful service restart, you should get something similar to the following:
|
1 2 3 |
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 27929/postgres |
And finally, the remote connectivity test:
|
1 2 |
# The remote login. psql 'host=myhost user=postgres password=mypassword' -c "SELECT 'hello world' AS greetings;" |
Then there is replication, but that is another blog altogether.
That is it for now.
Our white paper, “Why Choose PostgreSQL?”, looks at the features and benefits of PostgreSQL and presents practical usage examples. We also examine how PostgreSQL can be useful for companies looking to migrate from Oracle.
Resources
RELATED POSTS