In this Tutorial, we want to learn ” Tutorial PostgreSQL installation Ubuntu 20.04 ” and how it worked. So let’s start with what is postgres and when you know about that, install and work with postgres.
Table of Contents
What is PostgreSQL?
PostgreSQL is also called Postgres, which is an open-source, powerful, and relational database management system. PostgreSQL uses the SQL language with advanced features as concurrency without read locks and reliable transactions. Join us to learn the PostgreSQL installation on Ubuntu 20.04
The prerequisites before installation
To install PostgreSQL, first, you should have a configured Ubuntu 20.04 server, a basic firewall, and also a non_root user with Sudo permissions.
Tutorial PostgreSQL installation Ubuntu 20.04
Step by step to install Postgres on Ubuntu
Step 1.
You need to refresh your server’s local package index:
$ sudo apt update
To add utilities and functionality install the Postgres package with a -contrib:
$ sudo apt install postgresql postgresql-contrib
Step 2.
Use the PostgreSQL roles and Databases
After installation, Postgres would set up to use ident authentications to match the roles with Unix/Linux account.to authenticate and authorize, Postgres uses roles. While a role is created, a Unix/Linux username will be able to sign in as that role with the same name. now we verify the ways you can utilize the account to access Postgres.
First: by typing below command you will switch over to the Postgres account on your server, one way is to switch over to the Postgres account on your server:
$ sudo -i -u postgres
Then you will access the Postgres by typing:
$ psql
It would help to log into PostgreSQL to interact with the database management system.
If you need to exist out, you can use below command:
postgres=# \q
Also you can run the exit command to be back to system regular user:
postgres@server:~$ exit
Or run the psql command as the postgres account directly with sudo to connect the Postgres prompt without the intermediary bash shell in between:
$ sudo -u postgres psql
Again, you can exit the interactive Postgres session by typing:
postgres=# \q
Step 3.
Create a new role.
You can create a new role by typing:
postgres@server:~$ createuser --interactive
If you do not prefer to switch from your normal server account, use sudo for each command:
$ sudo -u postgres createuser --interactive
Due to your responses, run the correct Postgres your specifications commands to create a user to your specifications.
Output Enter name of role to add: sammy Shall the new role be a superuser? (y/n) y
Step 4.
Create a new Database.
The authentication system makes all the roles used to log in by default, so the roles will have a database with the same name to be accessible.
When you create a user with a name “sammy ” its role would attempt to connect to a database with the same name that you can create the database with createdb command.
Then if you log in with postgres account, you can type:
postgres@server:~$ creatdb sammy
If you do not prefer to switch from your normal server account, use sudo for each command:
$ sudo -u postgres created sammy
Step 5.
Open a Postgres with a new role.
You need a Linux user with the same name as your Postgres database and role when you want to go login with identity and if there is no matching Linux user, you can create a new one with adduser command. Using your non-root account with sudo privileges:
$ sudo adduser sammy
you can switch over and connect the database so long as the new account is available, by typing :
$ sudo -i -u sammy $ psql
Also you can do this inline:
$ sudo -u sammy psql
By running this command you will log in automatically if all the components have been configured properly. And by specifying database you would connect your user to a different database as below:
$ psql -d postgres
You are able to check the information of connection by typing:
Sammy=# \conninfo
Output
You are connected to database "sammy" as user "sammy" via socket in "/var/run/postgresql" at port "5432"
Dear user, you can ask questions about this training in the comments section, and get your answer, or to solve other problems in the field of Eldernode.com training, refer to the Ask page section and raise your problem in it as soon as possible. Make time for other users and experts to answer your questions.