Table of Contents
What you will read?
PostgreSQL is a powerful open-source relational database system widely used for applications, APIs, and enterprise projects. Debian 12 provides official packages for PostgreSQL, making it easy to set up and manage.
Step 1: Update System Packages
Make sure your system is up to date before installing:
sudo apt update && sudo apt upgrade -y
Step 2: Install PostgreSQL
Install PostgreSQL and related packages:
sudo apt install postgresql postgresql-contrib -y
This command installs the PostgreSQL server along with extra utilities and extensions.
Step 3: Enable and Start the Service
Ensure PostgreSQL runs automatically at startup:
sudo systemctl enable postgresql
sudo systemctl start postgresql
Check the status:
sudo systemctl status postgresql
Step 4: Switch to PostgreSQL User
PostgreSQL creates a default user named postgres. Switch to it:
sudo -i -u postgres
Now access the PostgreSQL shell:
psql
Exit the shell by typing:
\q
Step 5: Create a New Database and User
From the PostgreSQL prompt, you can create a new database and user:
CREATE DATABASE mydb;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Step 6: Allow Remote Connections (Optional)
To allow external access, edit:
sudo nano /etc/postgresql/15/main/postgresql.conf
Uncomment and set:
listen_addresses = '*'
Then update pg_hba.conf:
sudo nano /etc/postgresql/15/main/pg_hba.conf
Add this line:
host all all 0.0.0.0/0 md5
Restart PostgreSQL:
sudo systemctl restart postgresql
PostgreSQL is now installed and configured on Debian 12. The system is ready to manage relational databases, run SQL queries, and support scalable backend applications. For more Linux tutorials and guides, check out other articles on DROPVPS.
