DEV Community

Cover image for Installing PostgreSQL on Ubuntu 26.04
Aashish Chaurasiya for Vultr

Posted on • Originally published at docs.vultr.com

Installing PostgreSQL on Ubuntu 26.04

PostgreSQL is a powerful open-source relational database known for extensibility, standards compliance, and support for advanced data types including JSON and arrays. The PostgreSQL Global Development Group maintains a dedicated APT repository with the latest releases. This guide installs PostgreSQL from the PGDG repository, secures the installation, and creates a database with a dedicated user. By the end, you'll have a hardened PostgreSQL instance with a working database ready for application use.


Install PostgreSQL

The PostgreSQL Global Development Group provides an official APT repository with the latest PostgreSQL releases.

1. Update the APT package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install the PostgreSQL common package:

$ sudo apt install postgresql-common -y
Enter fullscreen mode Exit fullscreen mode

3. Run the repository setup script:

$ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Enter fullscreen mode Exit fullscreen mode

4. Install PostgreSQL:

$ sudo apt install postgresql -y
Enter fullscreen mode Exit fullscreen mode

5. Verify the installed version:

$ psql --version
Enter fullscreen mode Exit fullscreen mode

Manage the PostgreSQL Service

Enable PostgreSQL to start automatically when the server boots.

1. Enable and start the service:

$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql
Enter fullscreen mode Exit fullscreen mode

2. Check the service status:

$ sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

3. Stop or restart the service when needed:

$ sudo systemctl stop postgresql
$ sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

Secure PostgreSQL

1. Log in as the postgres superuser:

$ sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

2. Set a password for the postgres account:

ALTER USER postgres WITH PASSWORD 'your_strong_password';
Enter fullscreen mode Exit fullscreen mode

3. Create a new application user:

CREATE USER example_admin WITH PASSWORD 'secure_password';
\q
Enter fullscreen mode Exit fullscreen mode

4. Update the authentication method in pg_hba.conf:

$ sudo nano /etc/postgresql/18/main/pg_hba.conf
Enter fullscreen mode Exit fullscreen mode

Find the lines for local connections and change peer to scram-sha-256:

local   all             postgres                                scram-sha-256
local   all             all                                     scram-sha-256
Enter fullscreen mode Exit fullscreen mode

5. Restart PostgreSQL to apply changes:

$ sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

Create a Database and User

Log in as the postgres superuser and create a database owned by the application user:

$ sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode
CREATE DATABASE example_store OWNER example_admin;
GRANT ALL PRIVILEGES ON DATABASE example_store TO example_admin;
\q
Enter fullscreen mode Exit fullscreen mode

Create a Sample Table

Log in as the application user to verify access:

$ psql -U example_admin -d example_store -h localhost
Enter fullscreen mode Exit fullscreen mode
CREATE TABLE products (
    product_id SERIAL PRIMARY KEY,
    product_name VARCHAR(100) NOT NULL,
    category VARCHAR(100),
    price NUMERIC(10, 2)
);

INSERT INTO products (product_name, category, price) VALUES
    ('Widget A', 'Hardware', 9.99),
    ('Widget B', 'Software', 29.99),
    ('Widget C', 'Services', 49.99);

SELECT * FROM products;
\q
Enter fullscreen mode Exit fullscreen mode

All three rows in the query output confirm the database, user, and table are working correctly.


Next Steps

PostgreSQL is now installed and accepting connections. From here you can:

  • Use PostgreSQL as the database layer in a web application with Django or Rails
  • Set up streaming replication for high availability
  • Automate backups with pg_dump and a cron job

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (1)

Collapse
 
adnanmalik profile image
Adnan Malik

Clean tutorial. I have been working on a web tool (unit converter) and these server setup guides are always helpful for deployment.

The step-by-step format makes it easy to follow without skipping anything important.

Thanks for putting this together.