PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON storage, full-text search, and complex queries. This makes it a popular choice for everything from small personal projects to large production applications.
This guide shows you how to install PostgreSQL on Ubuntu, create users and databases, and set up remote access.
<strong>Prerequisite:</strong> You need a user account with sudo access.
Run these commands to install PostgreSQL and the contrib package. The contrib package adds extra tools and features that are useful for day-to-day database work:
bashsudo apt updatesudo apt install postgresql postgresql-contrib
The PostgreSQL service starts on its own after install. Verify it by checking the version:
bashsudo -u postgres psql -c "SELECT version();"
If you see a version string in the output, PostgreSQL is installed and working.
PostgreSQL manages database access using roles. A role can represent a single user or a group of users. Before you create any databases, it helps to understand how authentication works.
The most common authentication methods are:
pg_hba.conf allows it.scram-sha-256 and md5 formats.All authentication rules live in /etc/postgresql/12/main/pg_hba.conf. The default method for local connections is Peer.
When you install PostgreSQL, a system user called postgres is created. This is the database superuser. To open the PostgreSQL shell, run:
bashsudo su - postgrespsql
Or, without switching users:
bashsudo -u postgres psql
Type \q to exit.
Only superusers or roles with CREATEROLE privilege can create new roles.
Create a new database role:
bashsudo su - postgres -c "createuser john"
Create a new database:
bashsudo su - postgres -c "createdb johndb"
Open the PostgreSQL shell and grant the role access to the database:
bashsudo -u postgres psql
sqlGRANT ALL PRIVILEGES ON DATABASE johndb TO john;
By default, PostgreSQL only listens on 127.0.0.1. To accept connections from other machines, open the config file:
bashsudo nano /etc/postgresql/12/main/postgresql.conf
Find the listen_addresses setting and change it to:
listen_addresses = '*'
Save the file and restart PostgreSQL:
bashsudo service postgresql restart
Now edit pg_hba.conf to define who can connect and how. Here are some example rules:
# Allow user 'jane' from anywhere using a passwordhost all jane 0.0.0.0/0 md5# Allow user 'jane' from a trusted IP without a passwordhost all jane 192.168.1.134 trust
Open port 5432 in your firewall, allowing access from your trusted subnet only:
bashsudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432
PostgreSQL is now installed and ready to use on your Ubuntu server. You can manage roles and databases from the command line, and the authentication system gives you full control over who can access what. For more details, check the official PostgreSQL documentation. Questions? Leave a comment below.
Docker is an open-source platform that lets you package and run applications inside containers. Each container…
Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…
Apache Tomcat is an open-source web server and Java servlet container. It is one of the…
Keeping your Ubuntu system updated is one of the best ways to protect it. Security…
Intelligence analysts do not use OSINT only to collect information. They use it to turn…
OSINT reconnaissance is the first stage of ethical security research. Before testing anything, a security…