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 updateThen, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:
sudo apt install postgresql postgresql-contribEnsure that the service is started:
sudo systemctl start postgresql.serviceEnable the server
sudo systemctl enable postgresql.serviceStep 2: Using PostgreSQL Roles and Databases
sudo -i -u postgresThen you can access the Postgres prompt by running:
psqlThis 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:
\qThis will bring you back to the postgres Linux command prompt. To return to your regular system user, run the exit command:
exitAnother way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:
sudo -u postgres psqlThis 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:
\qStep 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:
psqlcreateuser --interactiveIf, instead, you prefer to use sudo for each command without switching from your normal account, run:
sudo -u postgres createuser --interactiveEither 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) yStep 4: Creating a new Database
createdb aryaIf, instead, you prefer to use sudo for each command without switching from your normal account, you would run:
sudo -u postgres createdb aryaStep 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 aryaOnce this new account is available, you can either switch over and connect to the database by running the following:
sudo -i -u arya
psqlOr, you can do this inline:
sudo -u arya psqlThis 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 postgresOnce 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".