Easy and Clear Tutorial: Install PostgreSQL on Ubuntu

PostgreSQL is a powerful, open-source, object-relational database management system (ORDBMS) with relational database features. It is a cross-platform database system that runs on multiple operating systems, including Linux, Unix, Windows, and Mac OS. It is suitable for a variety of application scenarios, from small projects to large enterprise applications. This tutorial will teach you how to install PostgreSQL Ubuntu.

Prerequisites to install PostgreSQL Ubuntu 22.04

OS: Ubuntu 22.04
Resources: 2-core CPU & 2GB of RAM
Access: SSH connection to the server
Permissions: a user with 'sudo' privileges

How to install PostgreSQL Ubuntu 22.04

Step 1: Install PostgreSQL on Ubuntu 22.04

Before installing PostgreSQL, refresh the local package index of the server:

$ sudo apt update

Then, install the Postgres package along with a -contrib package.
postgresql-contrib package: it provides additional utilities and functionality, commonly referred to as "contrib" modules. These modules include various extensions and tools that can enhance the capabilities of PostgreSQL.

$ sudo apt install postgresql postgresql-contrib

Next, press "Y" to confirm the installation. If you are prompted to restart any services, press “ENTER” to accept the default settings.
After the installation, ensure that the service is started:

$ sudo systemctl start postgresql

Finally, double-check that postgresql daemon is active:

$ service postgresql status

Step 2: Using PostgreSQL Roles and Databases

In PostgreSQL, roles and databases are essential components for managing access and organizing data.
When you install PostgreSQL on Ubuntu 22.04, a default admin user “postgres” is created by the default, which is associated with the default Postgres role. There are several ways to access Postgres using this account.

Method 1: Run the following command to switch to the postgres account on the server

sudo -i -u postgres

Then you can access the Postgres prompt by running:

$ psql

Then you can access the Postgres prompt by running:

$ psql

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.
To exit out of the PostgreSQL prompt, run the following:

\q

This will bring you back to the postgres Linux command prompt.
To return to your regular system user, run the exit command:

exit


Method 2: Run the psql command as the postgres account directly with sudo.
This will log you directly into Postgres without the intermediary bash shell in between.
To exit out of the PostgreSQL prompt, run the following:

\q

Step 3: Creating a New Role

If you're logged in as the postgres account, you can create a new role with the following command:

$ createuser –interactive

If you want to use each command without switching from your normal account, run the sudo command:

$ sudo -u postgres createuser –interactive

Either way, the script will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user to your specifications:

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

Step 4: Creating a New Database

Another assumption that the Postgres authentication system makes by default is that for any role used to log in, that role will have a database with the same name which it can access.
This means that if the user you created in the last section is called tommy, that role will attempt to connect to a database which is also called "tommy" by default. You can create the appropriate database with the createdb command.

If you are logged in as the postgres account, you would type something like the following:

$ createdb tommy

If you prefer to use each command without switching from your normal account, you would run sudo command:

$ sudo -u postgres createdb tommy

Step 5: Opening a Postgres Prompt with the New Role

To log in with ident based authentication, you’ll need a Linux user with the same name as your Postgres role and database.
If you don’t have a matching Linux user available, you can create one with the adduser command. You will have to do this from your non-root account with sudo privileges:

$ sudo adduser tommy

Once this new account is available, you can either switch over and connect to the database by running the following:

$ sudo -u tommy psql

This command will log you in automatically, assuming that all of the components have been properly configured.
If you want your user to connect to a different database, you can do so by specifying the database like the following:

\c tommy

Once logged in, you can get check your current connection information by running:

tommy=# \conninfo
You are connected to database "tommy" as user "tommy" via socket in "/var/run/postgresql" at port "5432".

Select Cloud Clusters for PostgreSQL Ubuntu

If you're looking for a Ubuntu PostgreSQL hosting service to help you save on tedious installation and administrative maintenance, consider Cloud Clusters' Postgresql Ubuntu.

Cloud Clusters' Postgresql Ubuntu offers highly optimized performance, 24/7 technical support, enhanced security and monitoring measures. By providing professional management, you can focus on business development without paying too much attention to the underlying server maintenance, while enjoying an efficient, secure, and reliable virtual server environment.