Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Collapse

Kakakuona Forum

  1. Home
  2. Blogs
  3. How to install PostgreSQL on Ubuntu OS With PgAdmin 4

How to install PostgreSQL on Ubuntu OS With PgAdmin 4

Scheduled Pinned Locked Moved Blogs
databasepostgrespostgreqslsqlubuntu
1 Posts 1 Posters 108 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SamohS Offline
    SamohS Offline
    Samoh
    wrote on last edited by
    #1

    PostgreSQL, often referred to as Postgres, is a powerful open-source object-relational database system. It is known for its robustness, scalability and support for advanced data types. in this guide, i'll walk you through the steps to install PostgreSQL on an Ubuntu operating system.

    Prerequisites

    Before we begin, ensure you have the following

    • A system running Ubuntu (20.04 or later)
    • A user account with sudo priviledges (Admin priviledge in the world of Windows)
    • An internet connection

    Step 1: Update your system

    Update your package list to ensure you have the latest information on the newest versions of packages and their dependencies

    sudo apt update
    

    Step 2: Install PostgreSQL

    i. Automated repository configuration:

    sudo apt install -y postgresql-common
    sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
    

    ii. Follow this steps to manually configure the APT repository. bored?{You can copy paste 🙂 }

    # Import the repository signing key
    sudo apt install curl ca-certificates
    sudo install -d /usr/share/postgresql-common/pgdg
    sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
    
    # Create the repository configuration file:
    sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
    
    # Update the package lists
    sudo apt update
    
    # Install the latest version of PostgreSQL
    # If you want a specific version, use 'postgresql-16' or similar instead of 'postgresql'
    sudo apt -y install postgresql
    

    Step 3: Verify the Installation

    Once the installation is complete, verify that PostgreSQL is running.

    sudo systemctl status postgresql
    

    You should see the status 'active', if not, you may need to repeat step 2

    b543e191-1f9c-4ecd-a1b6-a9932cf38608-image.png

    Yaay!, Now we have it. NOTE: To use PgAdmin 4, please skip to step 9

    Step 4: Access the PostgreSQL Command Line

    Now we need to switch to the PostgreSQL user account and access the PostgreSQL command line interface (CLI).

    sudo -i -u postgres
    # then to access the CLI
    psql
    

    You should now be at the PostgreSQL prompt (postgres=#), which looks like this (your terminal might be different 😉 )

    f2796375-9277-4413-bc1e-183725e727f4-image.png

    Step 5: Create a New Database User

    It's a good practice to create a new database user with superuser privileges for managing your databases.

    CREATE USER yourusername WITH SUPERUSER CREATEDB CREATEROLE PASSWORD 'yoursecurepassword';
    

    Replace yourusername and yourpassword with your desired username and password respectively.

    Step 6: Create a New Database

    Well, the reason you are installing this is to keep your application data at a centralized step right?

    CREATE DATABASE yourdatabase;
    

    Replace yourdatabase with your desired database name

    Step 7: Grant Privileges

    Grant all privileges on the new database to your new user (of course you should always grant necessary privileges to a user based on the needs/usage of the application, i'm just saying Lol! 🙂 )

    GRANT ALL PRIVILEGES ON DATABASE yourdatabase TO yourusername;
    

    Step 8: Exit the PostgreSQL CLI

    Exit the PostgreSQL command line interface.

    \q
    

    NOTE: This steps are for working with PostgreSQL locally, if you need to access your PostgreSQL server remotely, be free to leave a comment below.

    Installing PgAdmin 4

    Step 9: Installing PgAdmin 4 (On Ubuntu and Distros using DEB package manager)

    Install the public key for the repository (if not done previously):

    curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg
    

    Create the repository configuration file:

    sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
    

    Install pgAdmin (NOTE: If you have Apache running and don't want to interfere with it, install pgadmin4-desktop mode only)

    o Install for both desktop and web modes:

    sudo apt install pgadmin4
    

    o Install for desktop mode only:

    sudo apt install pgadmin4-desktop
    

    o Install for web mode only:

    sudo apt install pgadmin4-web
    

    Configure the webserver, if you installed pgadmin4-web:

    sudo /usr/pgadmin4/bin/setup-web.sh
    

    Now if you skipped step 4, we need to do the following (here using postgres as default user). On your terminal run

    sudo -u postgres psql
    

    Once you are inside psql (postgres=#)

    ALTER USER postgres WITH PASSWORD 'yourpassword';
    quit
    

    On your desktop, search for pgAdmin4

    b9146fd7-36ab-4822-90b0-06de7a61f77c-image.png

    Wait for some minutes for it to start, you should then be presented with the following window
    a4709caa-b445-4326-a0c6-99681409d92a-image.png

    On the Top-Left right click on Servers > Register > Server

    722648f2-d8eb-447c-b940-e94cbe2381c4-image.png

    Fill out necessary information. In the Name field under general, i write localhost

    e5c940b1-234d-4fc3-9689-659458483841-image.png

    Under Connection tab, in the Host name/address field, i write localhost (since im using it locally), i leave the rest with their defaults for this case. In the password field, put in your password created at step 9, then click on 'Save' button at the bottom right corner of the popup window.

    90e0cf55-033a-4cb1-9cc9-210e71ca98ed-image.png

    Now you should be presented with this nice view of pgAdmin4

    ed136174-a809-460e-8c8d-e37ec42e3567-image.png

    For now, this should get you up and running!

    I hope this steps have helped you gain some knowledge on installing PostgreSQL on Ubuntu OS.

    Thank you for reaching this far!

    If you find any errors, please feel free to comment them below.

    #Tanzania
    #kilimanjaro
    #uhurunaumoja

    IAMSamoh @cyb3rwolf

    1 Reply Last reply
    1

    • Login

    • Don't have an account? Register

    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups