Install PostgreSQL and pgadmin4 on your Ubuntu Easily

Install PostgreSQL and pgadmin4 on your Ubuntu Easily

So, Recently I had to rebuild my operating system, and I was having trouble setting up my development environment, which includes PostgreSQL, on the new installation. As you may know, I work a lot with PostgreSQL, and I needed to install it before I could do anything.

Luckily, I was able to set everything up correctly and get my Django applications up and running in no time.

If you are unfamiliar with PostgreSQL, It is a high-performance, enterprise-grade, open-source relational database system. SQL (relational) and JSON (non-relational) querying are both supported by PostgreSQL.

PostgreSQL is a very reliable database that has been developed by the open-source community for over 20 years.

Many online apps, as well as mobile and analytics applications, use PostgreSQL as their primary database

Here are some steps you may follow to simply install PostgreSQL on your PC if you wish to utilize it.

Installation

  1. First, update the package manager's cache with the following command

     $ sudo apt-get update
    
  2. Install PostgreSQL with the following command

     sudo apt-get install postgresql postgresql-contrib
    
  3. Enable and start Postgresql

     systemctl enable postgresql
     systemctl start postgresql
    

    Once the installation is complete, we can check that the service is running by using the following command

     sudo systemctl status postgresql
    
  4. By default, PostgreSQL creates a user named "Postgres" during the installation process. We can switch to this user by using the following command:

     sudo -u postgres psql
    

Using PostgreSQL Roles and Databases

"Roles" are a tool used by Postgres for authentication and authorization. The only user who can connect to the server by default is the Postgres user, which is created by Postgres. To create our superuser role to connect to the server.

sudo -u Postgres createuser --superuser $USER

After that, since Postgres by default expects a database with that $USER login name, we must build one.

$ sudo -u Postgres createdb $USER

We can create a new database and a new user with the following commands:

sudo su - postgres
createdb db_name
echo "CREATE ROLE db_user WITH PASSWORD 'DUDTL39YHa91x4Y';" | psql
echo "ALTER ROLE db_user WITH LOGIN;" | psql
echo "GRANT ALL PRIVILEGES ON DATABASE "db_name" to db_user;" | psql
exit

To enter inside Postgres, use this command:

psql -U postgres -h localhost

Some useful commands:

List database: \l

List users: \du

To exit the PostgreSQL shell, use the following command:

\q

To stop the PostgreSQL service, use the following command

sudo systemctl stop postgresql

Install pgadmin4

pgAdmin4 isn't accessible within the Ubuntu stores. We ought to introduce it from the pgAdmin4 Well-suited store. Begin by setting up the store. Include the open key for the store and make the store arrangement record.

$ curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
$ sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

Then install pgAdmin4,

$sudo apt install pgadmin4

Boom! It's Done.

That’s all! For more data, see the PostgreSQL documentation and pgAdmin 4 documentation. Keep in mind to share your considerations with us using the comment segment underneath.