Mastering PostgreSQL Installation on Ubuntu 22.04: A Comprehensive Step-by-Step Guide

Learn the essentials of installing PostgreSQL on your Ubuntu 22.04 system with our comprehensive guide. PostgreSQL is a robust and open-source relational database management system, and our step-by-step instructions ensure a smooth installation process. Tailored for both beginners and experienced users, this guide provides clear and concise directions to set up PostgreSQL on Ubuntu 22.04, empowering you to manage your databases effectively. Stay ahead in database management by following our user-friendly guide, which covers everything from package installation to initial configuration. Elevate your Ubuntu 22.04 environment by installing PostgreSQL seamlessly, and unlock the potential of a powerful, scalable, and reliable database solution for your projects. Prerequisites Ubuntu Server with 22.04 Version
Your server should have a non-root user with sudo permissions and a basic firewall.
Step 1: Installing PostgreSQL
To install PostgreSQL, first refresh your server’s local package index
sudo apt update
Then, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:
sudo apt install postgresql postgresql-contrib
Ensure that the service is started:
sudo systemctl start postgresql.service
Enable the server
sudo systemctl enable postgresql.service
Step 2: Using PostgreSQL Roles and Databases
sudo -i -u postgres
Then you can access the Postgres prompt by running:
psql
This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.
To exit out of the PostgreSQL prompt, run the following:
\q
This will bring you back to the postgres Linux command prompt. To return to your regular system user, run the exit command:
exit
Another way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:
sudo -u postgres psql
This will log you directly into Postgres without the intermediary bash shell in between.
Again, you can exit the interactive Postgres session by running the following:
\q
Step 3: Creating a new Role
If you are logged in as the Postgres account, you can create a new role by running the following command:
psql
createuser --interactive
If, instead, you prefer to use sudo for each command without switching from your normal account, run:
sudo -u postgres createuser --interactive
Either way, the script will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user to your specifications.
# Output
# Enter name of role to add: arya
# Shall the new role be a superuser? (y/n) y
Step 4: Creating a new Database
createdb arya
If, instead, you prefer to use sudo for each command without switching from your normal account, you would run:
sudo -u postgres createdb arya
Step 5: Opening a Postgres Prompt with the New Role
To log in with ident based authentication, you’ll need a Linux user with the same name as your Postgres role and database.
sudo adduser arya
Once this new account is available, you can either switch over and connect to the database by running the following:
sudo -i -u arya
psql
Or, you can do this inline:
sudo -u arya psql
This command will log you in automatically, assuming that all of the components have been properly configured.
If you want your user to connect to a different database, you can do so by specifying the database like the following:
psql -d postgres
Once logged in, you can get check your current connection information by running:
\conninfo
# Output
# You are connected to database "arya" as user "arya" via socket in "/var/run/postgresql" at port "5432".