PostgreSQL Installation on Ubuntu Server

Whenever I need to install PostgreSQL for trying out some new web framework or for setting up the VPS I have bought as a good deal, I need to find proper dense quick installation steps online. In my case, I always need to go through multiple guides because the first one is always missing something I need. So I decided to take notes and post them here for the future myself and for you if you find them useful.

What is PostgreSQL and why it is a popular choice for database management?

PostgreSQL, often simply called Postgres, is a free and open-source relational database management system (RDBMS). It is known for its strong reliability, robust feature set, and high performance.

Postgres is a popular choice for database management because it has a strong reputation for reliability and performance. It is also very flexible and can be used for a wide range of applications, from small projects to large, complex systems. Additionally, Postgres is open-source, which means that it is free to use and has a large, active community of contributors and users who can provide support and help develop new features.


The requirements for installing PostgreSQL on Ubuntu Server depend on the version of Ubuntu you are using and the version of PostgreSQL you want to install. However, in general, you will need the following:

  • A computer running Ubuntu Server. You can download and install Ubuntu Server from the Ubuntu website (https://ubuntu.com/download/server). Also, any Debian-based distro would work, and of course VPS installations too.
  • A user account with sudo privileges. This will allow you to run commands with superuser privileges, which may be required for some installation and configuration tasks.
  • Internet access. This is necessary to download the PostgreSQL packages from the internet.
  • Enough disk space to accommodate the PostgreSQL installation and any databases you create. The exact amount of space required will depend on the size and complexity of your databases.

You may also need to install additional dependencies, such as the libpq-dev package, which is required to build some PostgreSQL-related software. You can use the apt-get command to install dependencies like this:

sudo apt-get install libpq-dev

It’s a good idea to check the PostgreSQL documentation for the most up-to-date information on system requirements and dependencies. You can find the documentation on the PostgreSQL website (https://www.postgresql.org/docs/).

Update the package repository on Ubuntu.

sudo apt update

Install PostgreSQL and PostgreSQL-Contrib packages

sudo apt install postgresql postgresql-contrib

Start the PostgreSQL server and check if it is running

sudo systemctl start postgresql.service

sudo systemctl status postgresql.service

Switch to the postgres user. It should have been created during the PostgreSQL installation.

sudo -i -u postgres

Login to the Postgres Shell (Postgres Prompt) as the postgres user


Set password for the postgres user


Logout from the Postgres Shell


Create a new user for the database server. I will name it newdbuser

createuser --interactive

Create the default PostgreSQL database of the new user.

createdb newdbuser

If a system user with the same name doesn’t exist, create a new one.

sudo adduser newdbuser

Give sudo rights to the new system user.

usermod -aG sudo newdbuser

Login with newdbuser into PostgreSQL

sudo -u newdbuser psql

Set password for newdbuser


Create a database for the application.

CREATE DATABASE my_database;

List the databases on your PostgreSQL database server.


Exit from the Postgres Shell.


Logout from the postgres user


Tagged , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *