Setup PostgreSQL on Ubuntu 20.04 system

Setup PostgreSQL on Ubuntu 20.04 system

PostgreSQL is an object-relational database management system that provides an implementation of SQL querying language. It is a popular choice for many small and large projects. It comes in with rich features like table-inheritance and function overloading. Support for complex, concurrency and reliable ACID transactions.

In this blog, I'll tell you how to install PostgreSQL in your linux machine. So, let's get started!!

i] Update the system to it's latest version

Open up your terminal using a keyboard shortcut Ctrl+Alt+T.

sudo apt-get update

Later, make sure to upgrade all packages and dependencies.

sudo apt-get upgrade

ii]. Install Postgres package along with some additional utilities and functionalities

sudo apt install postgresql postgresql-contrib

The installation procedure created a user account called postgres that is associated with default postgres role. So in order to use PostgreSQL, we can log into that account.

There are two ways to access the PostgreSQL. Let us look at both ways.

1) Enter this command to open up the postgres command line.

sudo -i -u postgres

With this command, we will be using postgres user. In order to open up the interface, type: psql

In order to quit, just enter \q (backslash).

2) Now, another way to access the postgres interface is:

sudo -u postgres psql

Note: a. We can access database and user data with the help of command:

\conninfo

image.png

By default, both both database and user are named postgres.

b. Logout of postgres account using command logout.

iii] Creating a new user:

In order to create a new user(role) by running the following command:

sudo -u postgres createuser --interactive

image.png

iv] Creating a new database:

Create a new database using the given command:

sudo -u postgres createdb <db name>

image.png

v] Sign in with new role instead of postgres prompt

sudo -u <username> psql

It is important to note that the linux name in your system must be same as the postgres username. If you do not have the name for your Ubuntu machine, create user using command:

sudo adduser <username>

image.png

Note: Enter command man psql to look at all different commands and options.

vi] Install pgadmin3:

Click on Ubuntu Software icon and search for pgAdmin III. It is used for database management, administration and writing SQL queries.

image.png

vii] Conclusion:

Hurray!! We have successfully completed setting up PostgreSQL database on Ubuntu 20.04.