PostgreSQL, also known as Postgres is a free and opensource relational database management system. Unlike MySQL, and mssql it is an object-relational database as such it has more features like table inheritance, function overloading, and also it adheres more closely to SQL standards. This article guides you through the installation of Postgres on Ubuntu 20.04.
1. Prerequisite
Before we begin installing PostgreSQL. You’ll need to have Ubuntu 20.04 with sudo access enabled. If you haven’t already installed Ubuntu, please refer to this guide How To Install Ubuntu Server 20.04.
2. Add PostgreSQL repository
While PostgreSQL is available in the Ubuntu default repository, often that is not the latest released version. If you would like to have the latest release of PostgreSQL. Run the following command in your terminal to add PostgreSQL official repository.sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
3. Installation
Update the repository and install PostgreSQL by running the following command in your terminal.sudo apt-get update
sudo apt-get -y install postgresql
Once the installation is completed. The PostgreSQL service will start automatically. To check if the server is running, run the following command.sudo systemctl status postgresql
If the PostgreSQL server is not running you can run it using the following commands.sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo systemctl status postgresql
4. Connecting to database
Now as we have successfully installed PostgreSQL. let’s connect to Postgres and create a sample database. Unlike other database services, PostgreSQL uses a concept called “roles” to handle authentication and authorization. Postgres by default associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.
The installation procedure creates a user account called postgres that is associated with the default Postgress role. To use Postgress, you have to log in to that account.
sudo -iu postgres
You can now access a Postgress by typing:
psql
to exit out of the prompt, type the following command and hit enter.
q\
You can also connect directly without switching user by typing:
sudo -u postgres psql
5. Conclusion
You have successfully installed PostgreSQL server in Ubuntu 20.04 and connected to it through command line.Please let us know how the installation went for you? also, feel free to share your feedback and comments.