August 31, 2026 · by Alex M.
PostgreSQL is one of the most powerful and mature open-source relational database management systems (RDBMS) in the world. It stands out for its strict SQL standards compliance, full ACID transaction support, rich extensibility, and excellent performance at scale.
If you're running modern web applications, REST APIs, ERP systems, or analytics platforms on a NVMe VPS, PostgreSQL can be the ideal choice. This guide walks you through installing, securing, and optimizing PostgreSQL on a VPS running Ubuntu 22.04 or Debian 12.
Before diving into the installation, here's why PostgreSQL is gaining ground fast:
For serious projects running on an SSD VPS, PostgreSQL provides a solid and scalable foundation.
sudo privilegesIf you don't have a server yet, you can order a Linux VPS from CLIQHOST right now.
Before any installation, update the package list and system:
sudo apt update && sudo apt upgrade -y
This prevents dependency conflicts and ensures you're installing the most recent available packages.
sudo apt install postgresql postgresql-contrib -y
The postgresql-contrib package includes useful extensions: uuid-ossp, pg_stat_statements, hstore, and others.
To install the latest stable version (e.g., PostgreSQL 16):
# Install dependencies
sudo apt install -y gnupg2 wget
# Add GPG key
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Add repository
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | \
sudo tee /etc/apt/sources.list.d/pgdg.list
# Update and install
sudo apt update && sudo apt install postgresql-16 postgresql-client-16 -y
sudo systemctl status postgresql
psql --version
You should see something like: psql (PostgreSQL) 16.x
PostgreSQL automatically creates a system user called postgres, associated with the database superuser. Connect like this:
sudo -i -u postgres
psql
Or directly:
sudo -u postgres psql
You'll see the prompt postgres=#. Exit with \q.
This is the most critical step. An unprotected installation is a major security risk.
sudo -u postgres psql
ALTER USER postgres WITH PASSWORD 'VeryStrongPassword!2024';
\q
Never use the postgres superuser directly in applications. Create separate users with minimal privileges:
CREATE USER app_user WITH PASSWORD 'AppPassword!456';
CREATE DATABASE app_db OWNER app_user;
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;
The pg_hba.conf file controls authentication. Find it at:
sudo nano /etc/postgresql/16/main/pg_hba.conf
For secure local connections, ensure the local and 127.0.0.1 entries use scram-sha-256:
# TYPE DATABASE USER ADDRESS METHOD
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
After changes, restart the service:
sudo systemctl restart postgresql
If you don't need remote access to PostgreSQL, keep listen_addresses set to localhost in postgresql.conf:
sudo nano /etc/postgresql/16/main/postgresql.conf
listen_addresses = 'localhost'
If you need remote access (e.g., from a separate application server), specify the exact IP and protect port 5432 with a properly configured firewall.
-- List databases
\l
-- Create a database
CREATE DATABASE new_project;
-- Connect to a database
\c new_project
-- Drop a database
DROP DATABASE new_project;
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
-- Insert
INSERT INTO customers (name, email) VALUES ('John Doe', '[email protected]');
-- Read
SELECT * FROM customers WHERE email LIKE '%@example.com';
-- Update
UPDATE customers SET name = 'John Smith' WHERE id = 1;
-- Delete
DELETE FROM customers WHERE id = 1;
| Command | Description |
|---|---|
\l |
List databases |
\c <db> |
Connect to database |
\dt |
List tables |
\d <table> |
Table structure |
\du |
List users |
\timing |
Show query execution time |
\q |
Quit psql |
The default values in postgresql.conf are intentionally conservative. On a VPS with dedicated NVMe storage, you can tune them for better performance.
Open the configuration file:
sudo nano /etc/postgresql/16/main/postgresql.conf
# Memory
shared_buffers = 256MB # ~25% of available RAM
effective_cache_size = 768MB # ~75% of total RAM
work_mem = 16MB # per sort/hash operation
maintenance_work_mem = 128MB # for VACUUM, CREATE INDEX
# Connections
max_connections = 100 # adjust to your application's needs
# Checkpoint
checkpoint_completion_target = 0.9
wal_buffers = 16MB
# Useful logging
log_min_duration_statement = 500 # log queries taking > 500ms
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
After changes:
sudo systemctl reload postgresql
Use the online PGTune tool to automatically generate optimal parameters based on your RAM, CPU count, and workload type.
Regular backups are essential. PostgreSQL provides pg_dump and pg_dumpall for this purpose. If you need professional backup configuration, the CLIQHOST team offers server management services to handle everything for you.
sudo -u postgres pg_dump app_db > /backup/app_db_$(date +%Y%m%d_%H%M%S).sql
sudo -u postgres pg_dump -Fc app_db > /backup/app_db_$(date +%Y%m%d).dump
sudo -u postgres pg_dumpall > /backup/all_databases_$(date +%Y%m%d).sql
# From SQL file
sudo -u postgres psql app_db < /backup/app_db_20240115.sql
# From custom format (.dump)
sudo -u postgres pg_restore -d app_db /backup/app_db_20240115.dump
crontab -e
Add:
0 2 * * * sudo -u postgres pg_dump -Fc app_db > /backup/app_db_$(date +\%Y\%m\%d).dump 2>/var/log/pg_backup.log
-- Active queries
SELECT pid, now() - pg_stat_activity.query_start AS duration,
query, state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
-- Top slow queries (requires pg_stat_statements)
SELECT query, calls, total_exec_time, mean_exec_time
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;
sudo nano /etc/postgresql/16/main/postgresql.conf
shared_preload_libraries = 'pg_stat_statements'
Restart and activate the extension:
sudo systemctl restart postgresql
sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;"
SELECT pg_database.datname,
pg_size_pretty(pg_database_size(pg_database.datname)) AS size
FROM pg_database
ORDER BY pg_database_size(pg_database.datname) DESC;
PostgreSQL uses an MVCC storage mechanism that requires periodic cleanup via VACUUM:
-- Vacuum a specific table
VACUUM ANALYZE customers;
-- Full vacuum (locks the table!)
VACUUM FULL customers;
-- Reindex a table
REINDEX TABLE customers;
Autovacuum is enabled by default and handles this automatically. For large databases, you can fine-tune its parameters in postgresql.conf.
ssl = on in postgresql.confFor advanced server-level security and a fully managed environment, consider CLIQHOST's managed dedicated servers or our server administration services.
PostgreSQL is an excellent choice for any project that demands high data integrity, scalability, and flexibility. Once properly installed and configured on your Linux VPS, it becomes a reliable foundation for web applications, APIs, and business systems.
Need a high-performance server to run PostgreSQL? Explore CLIQHOST's NVMe VPS plans — fast NVMe storage, dedicated resources, and expert technical support included. Or browse our blog for more Linux server tutorials.
Have questions or need a hand? Contact our team — we're ready to help you build a solid infrastructure.
Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.
"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."
"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."
"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."