PostgreSQL is a database management system, similar to MySQL in many respects but with some key differences. Like MySQL, it’s commonly hosted on Linux. In this guide, we’ll show how to run a PostgreSQL server on Ubuntu 22.04 Jammy Jellyfish, as well as installing the client version in case you just need to connect to an external PostgreSQL database.
Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Install PostgreSQL Client
The PostgreSQL Client can be used to connect to an external PostgreSQL database. Use this option if you already have a database server up and running, but need to be able to remotely access the database from one or more client systems.
To get started, install the postgresql-client package by opening a command line terminal and entering the following two apt commands:
When the installation of PostgreSQL client is complete, you can use the psql command to connect to a remote PostgreSQL server. You’ll need to specify the hostname or IP address of the remote server (shown as postgre-server in the example below) and the username (postgre-user below) you’re authenticating with:
That’s it for the client version. In the next section, we’ll show how to set up a PostgreSQL server, which will be able to accept incoming client connections.
Install PostgreSQL Server
To get started hosting your PostgreSQL database, install the postgresql package on Ubuntu 22.04 with the following command:
$ sudo apt update
$ sudo apt install postgresql
Once PostgreSQL Server has finished installing, you should be able to see it listening for incoming connections on port 5432. This is a good way to confirm that it’s up and running as expected.
By default, PostgreSQL Server will start up automatically each time your system boots. If you’d like to change this behavior, you can always modify it with this command:
$ sudo systemctl disable postgresql
To re-enable it, just replace disable with enable.
PostgreSQL Server only listens on local loopback interface 127.0.0.1 by default. If you plan to have one or more remote clients connect to your database server, you’ll need to configure PostgreSQL to listen on a different network interface. To make this change, open PostgreSQL’s configuration file by using nano or your preferred text editor:
In this file, add the following line somewhere under the “CONNECTIONS AND AUTHENTICATION” section. This will instruct PostgreSQL to listen on all network interfaces for incoming connections.
Next, you should add the following line to your /etc/postgresql/14/main/pg_hba.conf configuration file, which will allow incoming client connections to all databases and users. The md5 option specifies that the users must authenticate with a password.
host all all 0.0.0.0/0 md5
To add this line to your file with a single command, just execute:
$ sudo bash -c "echo host all all 0.0.0.0/0 md5 >> /etc/postgresql/14/main/pg_hba.conf"
Lastly, if you have UFW firewall enabled, you can open PostgreSQL Server’s listening port 5432 to any incoming TCP traffic by executing the command below:
$ sudo ufw allow from any to any port 5432 proto tcp
Rule added
Rule added (v6)
In this tutorial, we learned how to host a PostgreSQL Server on Ubuntu 22.04 Jammy Jellyfish Linux. We also saw how to perform some initial configuration so our database would be able to accept incoming connections from any source and any user. In addition to this, we also saw how to use the PostgreSQL Client package to connect to a remote PostgreSQL server.