In addition to databases, there are services that maintain databases, called Database Management or DBMS. One of these DBMSs is calling PostgreSQL. This article will help you to get acquainting with PostgreSQL and learn How to Install and Connect PostgreSQL 12 on Ubuntu 22.04. You can check and purchase Ubuntu VPS server packages provided on the Eldernode website.
Table of Contents
Tutorial Install and Connect PostgreSQL 14 on Ubuntu 22.04
Introduction to PostgreSQL
PostgreSQL is a free and open source database management system based on SQL. This database supports SQL and JSON for its relational and non-relational queries for development with SQL. PostgreSQL can include a variety of advanced information and performance optimized features.
Features Provided in PostgreSQL 12
–> Improved high performance and workloads of distributed data.
–> Query parallelization and logical iteration.
–> Workloads with writing above.
–> Advances in synchronization.
–> Security enhancement.
In the rest of this article, we will teach you step by step how to Install and Connect PostgreSQL 12 on Ubuntu 22.04.
How to Install PostgreSQL on Ubuntu 22.04
First update and upgrade your system:
sudo apt update sudo apt -y install vim bash-completion wget sudo apt -y upgrade
Then reboot your system:
sudo reboot
Now import the GPG key using the following command:
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
You must add the new version of PostgreSQL to the default package repositories of the operating system:
apt policy postgresql
Enter the GPG key used to sign the packages:
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
Use the following command to add the PostgreSQL repository on your Ubuntu 22.04:
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
Run the command below to inform the system about the newly added repository:
sudo apt update
Now install PostgreSQL 12 on Ubuntu 22.04 with the following command:
sudo apt install postgresql-12 postgresql-client-12
You can check PostgreSQL status using the following command:
systemctl status postgresql.service
To set come up after every system reboot using the following command:
systemctl status [email protected]
How to Test PostgreSQL Connection
Note that a postgres user was created automatically during installation. First, give your account sudo privileges with the following command:
sudo su - postgres
Now you need to reset user password to a strong password:
psql -c "alter user postgres with password 'StrongAdminPassw0rd'"
Then start PostgreSQL by entering the following command:
psql
To get connection details use the below command:
\conninfo
Now you should create a test database and user with the following command:
CREATE DATABASE mytestdb;
CREATE USER mytestuser WITH ENCRYPTED PASSWORD 'MyStr0ngP@SS';
To grant all privileges on database mytestdb to mytestuser run the following command:
GRANT ALL PRIVILEGES ON DATABASE mytestdb to mytestuser;
You can list created databases by running the following command:
\l
Then connect to the database by running the following command:
\c mytestdb
You can use createuser to create users:
createuser myuser --password
To use createdb to create database using the following command:
createdb mydb -O myuser
Finally, run the following command:
psql -l
Conclusion
PostgreSQL is one of the services that maintain databases. In this article, you get acquainted with the new features in PostgreSQL 14 and also learned how to install and connect PostgreSQL 12 on Ubuntu 22.04 and we also examined configuration methods of PostgreSQL 14.