How to Install PostgreSQL on Ubuntu 20.04 LTS

To install PostgreSQL on Ubuntu 20.04 LTS, update your package lists, install PostgreSQL and its additional utilities, verify the service, and create a database and user for your application. This guide covers the essential commands for setting up PostgreSQL on an Ubuntu VPS or dedicated server. Whether you manage your own infrastructure or use a USA hosting provider, proper database configuration and access controls help keep your server hosting environment reliable and secure.

Prerequisites

Before installing PostgreSQL, prepare the following:

  • Operating system: Ubuntu 20.04 LTS
  • Resources: At least 2 CPU cores and 2 GB RAM for a basic setup; production requirements depend on workload.
  • Access: SSH access to your Ubuntu server
  • Permissions: A user with sudo privileges

Step 1. Install PostgreSQL on Ubuntu

Update the package lists:

sudo apt update

Install PostgreSQL and the additional utilities provided by postgresql-contrib:

sudo apt install postgresql postgresql-contrib

Start the service and enable it to launch at boot:

sudo systemctl enable --now postgresql

Verify that PostgreSQL is running:

sudo systemctl status postgresql

Check the installed client version:

psql --version

Ubuntu 20.04's default repositories provide a PostgreSQL version suitable for many applications. If your application requires a newer supported version, use the official PostgreSQL APT Repository.

Follow the repository's current installation instructions to configure its signing key and package source. Then update the package lists and install the required PostgreSQL version. Avoid outdated repository setup instructions that rely on the deprecated apt-key command.

Step 2. Access PostgreSQL and Manage Databases

The installation normally creates a Linux account named postgres and a corresponding PostgreSQL administrative role.

To open a PostgreSQL shell, run:

sudo -u postgres psql

You can now execute PostgreSQL commands.

List available databases:

\l

List database roles and their privileges:

\du

View the current connection information:

\conninfo

Exit the PostgreSQL shell:

\q

For local administrative access, Ubuntu commonly uses peer authentication, which checks the operating-system username. You do not necessarily need a PostgreSQL password when connecting through sudo -u postgres psql.

If you need to set a password for the PostgreSQL administrative role, enter the shell and run:

\password postgres

Use a strong, unique password. Setting a password does not automatically enable password authentication for every connection; authentication behavior depends on PostgreSQL's configuration.

Step 3. Create a New PostgreSQL Role

Create a role interactively:

sudo -u postgres createuser --interactive

Enter the desired role name when prompted. For example:

Enter name of role to add: tommy
Shall the new role be a superuser? (y/n) n

For normal application access, choose n for superuser privileges. Grant elevated permissions only when they are genuinely required.

You can also create a role directly in SQL:

CREATE ROLE tommy WITH LOGIN PASSWORD 'replace_with_a_strong_password';

Replace the example password with a strong, unique secret.

Step 4. Create a PostgreSQL Database

Create a database owned by the new role:

sudo -u postgres createdb -O tommy tommy

This creates a database named tommy and assigns ownership to the tommy role.

You can verify the database by opening PostgreSQL:

sudo -u postgres psql

Then run:

\l

Using a dedicated database and role helps separate application permissions and is a recommended practice for production database hosting.

Step 5. Connect Using the New Role

Ubuntu's default local peer authentication may require a matching Linux user when connecting through a local Unix socket.

If you want to use peer authentication, create a Linux account with the same name:

sudo adduser tommy

Then connect as that Linux user:

sudo -u tommy psql -d tommy

Alternatively, if password authentication is configured for the connection, connect by specifying the PostgreSQL role and database:

psql -h 127.0.0.1 -U tommy -d tommy

The command prompts for the role's password when required. The connection method depends on the applicable rules in pg_hba.conf.

After connecting, check the session:

\conninfo

To switch to another database from within psql, use:

\c tommy

Step 6. Secure Your PostgreSQL Server

Before deploying an application, review these security settings:

  • Limit network access: PostgreSQL uses port 5432 by default. Keep it private unless remote connections are required.
  • Configure authentication: Review pg_hba.conf and use appropriate authentication methods.
  • Apply least privilege: Avoid using the postgres superuser for routine application connections.
  • Maintain updates: Install security updates and use a PostgreSQL version supported by your application and maintenance requirements.
  • Back up your data: Schedule database backups and test restoration procedures.

If remote access is required, configure PostgreSQL's listening addresses and authentication rules carefully, and restrict the relevant firewall rules to trusted sources.

These practices are important whether PostgreSQL runs on an individual VPS, a dedicated server, or infrastructure managed by a hosting provider.

Conclusion

You have now installed PostgreSQL on Ubuntu 20.04 LTS, verified the service, and learned how to create a role and database. The same core workflow applies to many Ubuntu VPS hosting environments.

For production use, configure authentication and firewall rules, apply the minimum permissions required by your application, and maintain tested backups. If you need a ready-to-use Ubuntu server, explore Database Mart's Ubuntu VPS Hosting for a server environment suitable for PostgreSQL and other database-driven applications.

Last Updated:   10/23/2026
Outline