PostgreSQL (Postgres) is an open-source, general-purpose object-relational database management system with advanced capabilities for building fault-tolerant systems and complex applications.
In this tutorial, you will learn how to install PostgreSQL on Ubuntu 20.04.
1.Connect to your server and run system updates:
sudo apt update
2.Install the PostgreSQL package along with the -contrib package for additional utilities and functionality:
sudo apt install postgresql postgresql-contrib
3.Ensure the PostgreSQL service is running:
sudo systemctl start postgresql.service
4.PostgreSQL uses “roles” for authorization and authentication. Switch to the postgres user:
sudo -i -u postgres
5.Access the PostgreSQL prompt:
psql
You will be logged into the PostgreSQL prompt, where you can interact with the database management system. To exit the prompt, use:
\q
6.Create a new role:
If you are logged in as the postgres user, create a new role by running:
createuser --interactive
Enter the name of the new role and assign superuser privileges.
7.Create a database:
By default, each role used to log in will have a database with the same name. To create this database, use:
createdb test
Alternatively, if you prefer to use sudo for each command, you can run:
sudo -u postgres createdb test
8.Create a matching Linux user:
You'll need a Linux user with the same name as your Postgres role and database to log in with ident-based authentication. Create this user with:
sudo adduser test
9.Connect to the database:
Switch to the new user and connect to the database:
sudo -i -u test
psql
10.Connect to a different database (optional):
To connect to a different database, specify the database name:
psql -d postgres
11.Check your current connection information:
Once connected, you can view your connection details by running:
\conninfo