One essential step for anyone working on database administration. Else working on application development on Ubuntu 20.04 is installing PostgreSQL. The open-source relational database management system PostgreSQL is praised. Praised for both its conformance to SQL standards and its extensibility. Assuring a solid foundation for storing and managing your data. Regardless of your level of experience as a database administrator or developer.
An implementation of the SQL querying language is offered by the relational database management system known as PostgreSQL. Along with many more sophisticated features like concurrency without read locks. It is dependable transactions, it complies with standards.
This tutorial shows you how to rapidly install PostgreSQL on an Ubuntu 20.04 server. Also, set up a new user and database to show you how to use Postgres. See How To Install and Use PostgreSQL on Ubuntu 20.04. For a more thorough walkthrough on setting up and maintaining a PostgreSQL database.
Required Conditions
- You will need one Ubuntu 20.04 server that has been configured using our Initial Server Setup for Ubuntu 20.04 guide.
- Your server should have a simple firewall and a non-root user with sudo capabilities when you finish this required instruction.
Install PostgreSQL on Ubuntu 20.04 | 22.04
Step 1: The First thing is to Install PostgreSQL
Refresh the local package index on your server before installing PostgreSQL:
$ sudo apt update
Install the Postgres package after that, along with the -contrib package, which adds some more tools and features:
$ sudo apt install postgresql postgresql-contrib
Make sure the service has begun:
$ sudo systemctl start postgresql.service
Step 2: Using PostgreSQL Roles and Databases
By default, Postgres handles authorization and authentication using a notion known as “roles.” These resemble typical Unix-style users and groups in certain aspects.
Postgres is configured to employ ident authentication by default, which links Postgres roles to corresponding Unix/Linux system accounts. A Unix/Linux username with the same name can log in as that role if it exists within Postgres.
A user account named Postgres was established during the installation process and is connected to the Postgres role by default. You can use this account to access Postgres in a few different ways. One method is to use the following command to switch your server to the Postgres account:
$ sudo -i -u postgres
After that, run the following to get to the Postgres prompt:
$ psql
This will launch the PostgreSQL prompt, where you can immediately begin interacting with the database management system.
Use these commands to close the PostgreSQL prompt:
postgres= # \q
This will return you to the Linux command prompt for Postgres. Use the exit command to revert to your default system user:
postgres@server:~$ exit
Using sudo to run the psql command as the postgres account directly is an additional method of connecting to the Postgres prompt:
$ sudo -u postgres psql
This will log you into Postgres directly, bypassing the need for an intermediate bash shell.
Once more, you can use the following commands to end the interactive Postgres session:
postgres=# \q
Step 3: Forming a New Position {followed by step 2}
The following command can be used to establish a new role if you are logged in as the postgres account:
postgres@server:~$ createuser --interactive
Alternatively, if you would rather use sudo instead of changing to a different account for every command, execute:
$ sudo -u postgres createuser --interactive
In either case, the script will present you with a few options and then, in response to your selections, will run the appropriate Postgres commands to create a user according to your preferences.
Step 4: Establishing a New Database
The Postgres authentication mechanism additionally assumes that any role used to log in will have a database with the same name that it may access by default.
This implies that the role will try to connect to a database that is likewise named “xyz” by default if the user you created in the previous section is called Xyz. To create the required database, use the createdb command.
If you were using the postgres account to log in, you would type something like this:
postgres@server:~$ createdb xyz
Alternatively, if you’d rather use sudo instead of changing to a different account for every command, you would run:
$ sudo -u postgres createdb xyz
Step 5: Using the New Role to Open a Postgres Prompt
You must create a Linux user with the same name as your Postgres role and database to log in using identity-based authentication.
If there isn’t an existing Linux user that matches, you can make one by using the adduser command. This needs to be done using your non-root account with sudo capabilities, which means you can’t log in as postgres:
$ sudo adduser xyz
When the new account is ready, you may use one of the following methods to switch over and establish a connection with the database:
$ sudo -i -u xyz $ psql
Or, you could complete this inline:
$ sudo -u xyz psql
Assuming that every component has been set up correctly, this command will immediately log you in.
You can define the database in the following way if you want your user to connect to a different database:
$ psql -d postgres
After logging in, use the following command to see the details of your current connection:
xyz=# \conninfo
image
Wrapping Up about Installing PostgreSQL on Ubuntu 20.04
To sum up, PostgreSQL on Ubuntu 20.04 may now be successfully installed. Opening the door to a robust and capable database management system. This guide’s instructions provide an easy approach to setting up a PostgreSQL environment. That will surely suit your data storage requirements. After installing the program, you have a vital tool for creating and managing effective databases.
Accept PostgreSQL’s potential to improve the speed and scalability of your apps. Secure in the knowledge that you’ve set up a strong framework. For efficient data handling on your Ubuntu 20.04 system.
PostgreSQL is now configured on your Ubuntu 20.04 server. We invite you to review the following instructions if you want to learn more about Postgres and how to utilize it. To understand on How to Install MySQL on Ubuntu, please refer to this blog post.