Update and Upgrade the System Repositories
Letās get started with the update of a system before the installation of Postgres on the Ubuntu system. This is required to refresh the system repositories via the āupdateā instruction and the āaptā package with the sudo rights.
After successfully updating the Ubuntu system, itās necessary to upgrade the system repositories and modules to avoid any inconvenience in the future. In the very same way, the apt repository is used in the āupgradeā command as follows:
Install the Prerequisites
Now that we will install PostgreSQL from its official repository, we need to ensure that the prerequisites are already installed. These prerequisites include the āwgetā utility to add the Postgres key in Ubuntu and āca-certificatesā to enable the installation by enabling the certificates for secure installation through the official repositories. As depicted in the following image, the āapt-getā installation instruction is used for this purpose.
Add the PostgreSQL GPG Key to Ubuntu
After installing the āwgetā Ubuntu utility and the ca-certificates package, we move towards adding the Postgres GPG key to the Ubuntu system. As mentioned, we will utilize the āwgetā utility with the āāquietā and ā-Oā options followed by the URL of the official Postgres repository site to add its GPG key to the Ubuntu system. The main part of this instruction is using the āapt-keyā keyword followed by the āaddā keyword. The output of this command displays āOKā in return. The Postgres GPG key is successfully added to the system, and you can finally install it.
After adding the Postgres GPG key to the Ubuntu system, it’s necessary to update the system packages and repositories once again so that the system can take the effect of the GPG key perfectly. Therefore, we execute the āupdateā instruction again.
Install PostgreSQL on Ubuntu
After perfectly meeting the pre-requirements of Ubuntu for the installation of Postgres, our system is now ready to install PostgreSQL. To install PostgreSQL on the Ubuntu system, we will utilize the everlasting āapt-getā package within the āinstallā command. The command uses the āpostgresqlā package name followed by the additional package which is āpostgresql-contribā which contributes an extra functionality to the PostgreSQL database.
The installation process continues until it prompts for the affirmation. To proceed with the remaining installation process without interruption, tap on āyā when prompted at the command shell. To top the execution right at the moment, tap on āyā.
The process of PostgreSQL installation creates a system link between PostgreSQL and system files. It also displays the username for a new database user that is generated automatically right after the installation of PostgreSQL. After displaying a certain information regarding PostgreSQL, it displays āOKā and quits. The process displays that Postgres version 12 is successfully installed through the developer site.
If you donāt want to use the version of Postgres that is available at the developer site and you want to go with the available local version, you can also look for that. The āapt showā instruction displays the latest version of Postgres, i.e. āVersion 12ā. You can install it via the previous installation command that you utilized to get it from the official site.
Now that PostgreSQL is successfully installed in the Ubuntu system, we start its service via the āsystemctlā utility and look for its current status, i.e. running or not. The output of the status instruction displays that the PostgreSQL has been active.
$ sudo systemctl status postgresql
Connect to PostgreSQL
Now, itās time to use PostgreSQL and connect with its databases. The āsuā instruction is used with the āpostgresā keyword to open PostgreSQL. After successfully launching PostgreSQL, we need to launch the PostgreSQL command utility. For this, use the āpsqlā instruction, and the command prompt will be launched.
$ psql
We are currently logged in with the āpostgresā database using the āpostgresā user as per the ā\conninfoā command that is used in the Postgres command prompt.
Create a Database
Letās make a new database in the PostgreSQL database. For this, make sure to open Postgres via the āsuā instruction and utilize the ācreatedbā instruction with the database name to be created.
You can log in from a specific database and quit it via the ā\qā command. After returning to the PostgreSQL main menu, you can exit it via the āexitā instruction.
$ exit
Let’s say we added a new user, ātestā, in our Ubuntu system via the āadduserā instruction and didnāt assign it with any sudo or administrative rights.
Switch the Database
Now, to open Postgres with the newly created ātestā user, we need to switch the user in our command shell first using the ā-Iā and ā-uā option. Upon opening PostgreSQL with the ātestā user, it doesnāt allow us because the ātestā user is not a sudo user.
$ sudo su ā postgres
On the other hand, you can successfully login from PostgreSQL via any other user with the āsudoā rights, i.e. xyz. Now, you can switch the database as well. Thus, we utilize the āpsqlā instruction to switch to the newly created ātestā database.
$ sudo su ā postgres
$ psql -d test
The following instruction displays the connection information:
The SQL command is the same as any conventional database, i.e. create table.
To display the list of tables in a PostgreSQL database, run the ā\dā instruction as follows:
Conclusion
This guide demonstrates the importance and compatibility issues of Postgres during installation on Ubuntu. After this, we discussed a simple and brief way to install Postgres on Ubuntu like installing the tools from the PostgreSQL Apt Repository. Ultimately, we elaborated the simplest method to use PostgreSQL and the switch databases.