Install and Configure PostgreSQL on Ubuntu : A Complete 2025 Guide
Master PostgreSQL setup on Ubuntu! This comprehensive guide walks you through installation using APT, setting the superuser password, creating users, managing roles, and running essential commands to verify your setup.

✅ Introduction
PostgreSQL is one of the most powerful and reliable open-source relational database systems available today. Whether you're setting up a development environment or preparing a production server, PostgreSQL on Ubuntu is a rock-solid choice. This guide covers everything from installation to user management and database operations.
📥 1. Installing PostgreSQL on Ubuntu Using APT
PostgreSQL 14–17
Ubuntu 20.04, 22.04, and 24.04 LTS
Ubuntu’s package manager makes it easy to install PostgreSQL.
Step 1: Update system packages
sudo apt update && sudo apt upgrade -yStep 2: Install PostgreSQL
sudo apt install postgresql postgresql-contrib -yStep 3: Verify PostgreSQL status
sudo systemctl status postgresqlThe PostgreSQL server should now be running.
🔐 2. Set PostgreSQL Superuser (postgres) Password
By default, PostgreSQL uses a role-based authentication and creates a user called postgres.
Switch to the postgres user:
sudo -i -u postgresEnter PostgreSQL prompt:
psqlSet the password:
\password postgresExit psql and return to your system user:
\q exit👥 3. Create a New PostgreSQL User and Assign Password
To add your own user:
sudo -i -u postgres createuser --interactiveFollow the prompt to name your user and choose whether to grant them superuser privileges.
OR create user with password directly:
sudo -i -u postgres psqlCREATE USER myuser WITH PASSWORD 'strongpassword';🎛️ 4. Grant Roles and Permissions
Grant access to databases or privileges:
ALTER USER myuser CREATEDB; -- Or to grant all privileges: GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;🧪 5. Create Database and Tables (Test Setup)
Create a database:
CREATE DATABASE mydb;Connect to the database:
\c mydbCreate a table:
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE );Insert sample data:
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');View table contents:
SELECT * FROM users;🧼 6. Exit PostgreSQL and Test as New User
Exit PostgreSQL:
\q exitTry logging in with your new user:
psql -U myuser -d mydb -h 127.0.0.1 -W✅ Conclusion
You now have PostgreSQL installed and configured securely on Ubuntu. From setting passwords to creating users and running queries, this setup gives you a strong foundation for development or production use.
Share this article
Admin User
Admin User
Related Posts

How to Set Up Redis in Node.js on Ubuntu 20.04/22.04/24.04 LTS
Learn how to install and configure Redis with Node.js on Ubuntu 20.04, 22.04, or 24.04 LTS. This step-by-step guide ensures high-performance caching and data storage for your Node.js apps.

The Enduring Importance of Data Structures & Algorithms
In an age where AI seems to handle everything from chatbots to complex simulations, the fundamentals of Data Structures & Algorithms (DSA) remain the cornerstone of every efficient, scalable, and innovative software solution. Here’s why, even in 2025, DSA skills are non-negotiable for any developer aiming to thrive.

Average Salaries in India (2025) for Developers & Designers Based on Experience
Planning a career in tech or design? This detailed guide breaks down the average salary in India for developers (Node.js, Java, Python, Django) and designers based on experience—from fresher to 4+ years.

Ubuntu 24.04 LTS Installation Guide for Any PC – From BIOS to Developer Setup
Want to install Ubuntu 24.04 LTS on your PC? This universal guide covers BIOS setup, swap memory recommendations, partitioning, and essential post-install commands for all systems.
