If we talk about the relational database management system then PostgreSQL is one of the popular. It is not only relational database management system. It is is an open source object-relational database system. This database has a strong reputation for reliability, feature robustness, and performance. It is very much similar to MySQL database in use. You can easily install PostgreSQL in any platform. If you are a Windows user then you can download PostgreSQL installer file and install it. But, if you are a Linux user or MAC user then it won’t work the same installation process. So, in this post, I will show how you can install PostgreSQL in Ubuntu 20.04. I will start from very scratch so, let’s continue to the post.
I assume you are ready with the below requirements to proceed to install postgres.
- Ubuntu 20.04.2 LTS (As an Operating System)
- An Ubuntu user with Sudo Privileges
- A terminal and command-line access
Check Ubuntu APT Updates Repository
When you are ready to install Postgres in Ubuntu then you please check the repository updates. So, just open the terminal and hit the below command there.
sudo apt update
This will check the available updates in Ubuntu repository. If any updates will found, it will list out.
Make sure, you have upgraded these updates. To upgrade the package from the repository, you will have to enter the below command.
sudo apt upgrade
After upgrading the packages, you can start to install PostgreSQL in Ubuntu.
Install PostgreSQL in Ubuntu
The PostgreSQL package is available by default in Ubuntu apt repository. Also, if you don’t have the version choice during the installation then you may leave it by default. It will install the latest version automatically. Hence, simply hit the below command in the terminal.
sudo apt install postgresql postgresql-contrib
After hitting the above command, it will start the installation process.
After the installation, we will check if the postgres is installed or not.
How to Check PostgreSQL in Ubuntu
If the installation is completed then you can check the version in the terminal.
sudo apt show postgresql
The above command will show you the complete description like package, version, priority, and many more things.
Now, in the next step, we will login into the PostgreSQL as a super user. Actually, by default there is no password for the database user. And the default user is postgres.
How to Access PostgreSQL Database
To enter into the PostgreSQL database, you can use the sudo command as showing below.
sudo -u postgres psql
In this command, the user is postgres and there is no password.
If you want to exit from the postgres terminal then simply enter \q. It will quit the window.
The second way of accessing PostgreSQL is simply hit the below command.
$ sudo -i -u postgres psql
It will prompt you the same terminal. As, I told you here the user in PostgreSQL is postgres and password is empty. Just similar to the above line.
So, here we entered into PostgreSQL without the password.
Check Connection in PostgreSQL
If you want to check the current connection detail of the logged user then you can check it.
$ sudo -u postgres psql postgres=# \conninfo
After logging with user postgres simply hit the \conninfo.
Create User in PostgreSQL
You can create a new user in Postgres by hitting the below command. Here, I am going to create a new user without logging into the postgres.
sudo -u postgres createuser --interactive
After hitting the command, it will prompt you the name of the user that you want to create. After giving the name, it will ask for the role of the superuser. As we know the superuser has all the permission for managing the database. The superuser can create new user, set the role for database and many more options.
Here, I have given the permission of superuser to new user. If you don’t give the superuser role then it will ask you the next prompt. So, you have to just follow the options further.
Check PostgreSQL Service Status in Ubuntu
In Ubuntu, sometimes the running Postgres services may stop due to some conflict. That time, you won’t be able to access PostgreSQL in your system. So, you will have to check whether the service is running or not.
sudo service postgresql status
It will show you the current status of the Postgres service.
Here, currently the Postgres status is in running mode. You can enable or disable it if you required.
Start/Stop PostgreSQL Service in Ubuntu
If you want to start or stoop the PostgreSQL service then use the below command.
sudo service postgresql stop/start
After that check the status. Here, I stopped the service and checked back. You can see, it is stopped.
The PostgreSQL is fully installed in Ubuntu. We created the new user in PostgreSQL. Also, I have shown you to check the connection info, service status, start and stop the service. Here, now you can create the database and manage it accordingly. This is very popular database and pretty similar to MySQL in use. So, I hope you will enjoy this post and find it helpful.