To install PostgreSQL on Ubuntu 22.04, follow these steps:
Update the package manager's package list by running the following command:
sudo apt update
Install the PostgreSQL package by running the following command:
sudo apt install postgresql
After the installation is complete, start the PostgreSQL service by running the following command:
sudo systemctl start postgresql
To make sure that the PostgreSQL service starts automatically when the system boots, run the following command:
sudo systemctl enable postgresql
To check the version of PostgreSQL installed on your system, run the following command:
For Server version:
pg_config --version
For Client version:
psql --version
To log in to the PostgreSQL database as the default "postgres" user, run the following command:
sudo -u postgres psql
This will open the PostgreSQL interactive terminal. From here, you can create databases, users, and perform other tasks. For example, to create a new database called "mydb_name", run the following command:
postgres=# create database mydb_name;
To create a new user called "myuser" with the password "mypass", run the following command:
postgres=# create user myuser with encrypted password 'mypass';
To grant all privileges on the "mydb_name" database to the "myuser" user, run the following command:
postgres=# grant all privileges on database mydb_name to myuser;
If you want to log in as a different user, you can use the following command:
psql -U username
Replace "username" with the name of the user you want to log in as.
That's it! You have successfully installed and configured PostgreSQL on Ubuntu 22.04. You can now use the interactive terminal or connect to the database from your application to perform various tasks.
Top comments (0)