
September 09, 2026
11 min read
By Kokil Thapa | Last reviewed: September 2026
PostgreSQL Administration Essentials is the set of daily tasks that keep a database alive after launch: install, secure, back up, tune, monitor, and recover. Most teams pick PostgreSQL for JSON support, strict SQL, and predictable concurrency. Then they treat it like a black box until disk fills or queries crawl. I've maintained PostgreSQL on production Laravel applications and booking systems where downtime means lost orders. This guide walks through the admin work I actually do on Ubuntu servers—not theory from a certification slide deck.
What does PostgreSQL administration cover for production web apps?
Administration sits between application code and the operating system. Your Laravel or Symfony app talks through a connection pool. PostgreSQL handles rows, locks, and WAL files on disk. The Linux host supplies CPU, RAM, and I/O.
A production admin stack has six layers. Skip any one and you will feel it during Dashain traffic spikes or a failed deploy.
- Installation and access control — correct version, limited network exposure, role-based grants
- Backup and recovery — pg_dump schedules plus WAL archiving for point-in-time restore
- Performance tuning — shared_buffers, work_mem, indexes, and autovacuum settings
- Monitoring — connection counts, slow queries, disk growth, replication lag
- Maintenance — vacuum, reindex, extension updates, major version upgrades
- Security — TLS, pg_hba.conf rules, least-privilege app users, audit logging
If you are choosing between engines, read PostgreSQL vs MySQL for production first. PostgreSQL rewards teams that plan maintenance windows. MySQL often feels simpler on shared hosting. Both fail the same way when nobody owns backups.
On booking platforms like Adventure Third Pole Trek, the database holds itineraries, payments, and supplier records. Admin work is not optional. It is part of shipping reliable software.
How do you install and configure PostgreSQL 18 on Ubuntu?
Ubuntu 24.04 ships PostgreSQL packages from the PGDG apt repository. Pin the major version you want. PostgreSQL 18 is current. PostgreSQL 17 remains common on managed hosts. Do not mix versions across app servers and replicas.
Add the official PostgreSQL apt repository
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install -y postgresql-18 postgresql-client-18 Confirm the cluster status:
sudo systemctl status postgresql
sudo -u postgres psql -c "SELECT version();" Create application roles with least privilege
Never point Laravel at the postgres superuser. Create a database, an owner role, and a separate migration role if you use schema-only deploys.
sudo -u postgres psql <<'SQL'
CREATE ROLE app_owner LOGIN PASSWORD 'replace-with-strong-secret';
CREATE ROLE app_user LOGIN PASSWORD 'replace-with-strong-secret';
CREATE DATABASE myapp_db OWNER app_owner ENCODING 'UTF8';
GRANT CONNECT ON DATABASE myapp_db TO app_user;
SQL Inside the database, grant schema usage explicitly:
sudo -u postgres psql -d myapp_db -c "GRANT USAGE ON SCHEMA public TO app_user;"
sudo -u postgres psql -d myapp_db -c "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;" Lock down pg_hba.conf and listen_addresses
Edit /etc/postgresql/18/main/postgresql.conf and set listen_addresses to the private interface IP—not * on a public VPS. Then restrict clients in pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
host myapp_db app_user 10.0.1.0/24 scram-sha-256
hostssl myapp_db app_user 10.0.1.0/24 scram-sha-256 Reload after changes:
sudo systemctl reload postgresql For full server hardening beyond the database, see Linux system administration practices. Firewall rules with UFW should allow port 5432 only from app subnets.
| Setting | Dev default | Production starting point | Why it matters |
|---|---|---|---|
listen_addresses | localhost | Private IP only | Stops random internet scans hitting 5432 |
ssl | off locally | on with valid cert | Encrypts credentials and row data in transit |
log_min_duration_statement | -1 (off) | 500–1000 ms | Surfaces slow queries without log floods |
max_connections | 100 | 100–200 + pooler | Laravel opens many idle connections under load |
shared_buffers | 128 MB | 25% of RAM (cap ~8 GB) | Primary page cache for hot data |
Official install docs live at postgresql.org/download/linux/ubuntu. Cross-check package names before you paste commands into CI.
How do you back up and restore PostgreSQL databases safely?
Backups are the heart of PostgreSQL Administration Essentials. A logical dump via pg_dump is simple. Physical base backups plus WAL archiving give you point-in-time recovery. You need both skills.
Schedule logical dumps with pg_dump
For small and mid-size Laravel apps under roughly 50 GB, nightly custom-format dumps work well. Store them off-server.
#!/bin/bash
BACKUP_DIR="/var/backups/postgresql"
STAMP=$(date +%Y%m%d_%H%M)
mkdir -p "$BACKUP_DIR"
pg_dump -Fc -U app_owner -d myapp_db -f "$BACKUP_DIR/myapp_db_$STAMP.dump"
find "$BACKUP_DIR" -name "*.dump" -mtime +14 -delete Test restores monthly on a staging VM. A backup you have never restored is a guess.
pg_restore -C -d postgres /var/backups/postgresql/myapp_db_20260909_0200.dump Enable WAL archiving for point-in-time recovery
When you need to rewind to 14:07 before a bad migration, WAL files are the only path. Enable archiving in postgresql.conf:
archive_mode = on
archive_command = 'test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f'
wal_level = replica Take a base backup with pg_basebackup:
pg_basebackup -D /var/lib/postgresql/18/base_backups/$(date +%F) \
-Ft -z -P -U replication_user -h localhost Our detailed PostgreSQL point-in-time recovery playbook walks through full restore steps. Read it before your first incident, not during one.
If you are moving from MySQL, plan the cutover with our MySQL to PostgreSQL migration guide. Dump-and-restore timing affects downtime windows.
How do you tune PostgreSQL performance for Laravel and API workloads?
Most Laravel slowness is N+1 queries or missing indexes—not mysterious PostgreSQL bugs. Still, baseline server tuning prevents pain as tables grow.
Memory settings that actually move the needle
On a 8 GB RAM VPS running only PostgreSQL:
shared_buffers = 2GB
effective_cache_size = 6GB
work_mem = 16MB
maintenance_work_mem = 512MB
random_page_cost = 1.1 # for SSD/NVMe Restart PostgreSQL after changing shared_buffers. Reload is enough for most other knobs.
Autovacuum and bloat
PostgreSQL uses MVCC. Dead row versions pile up without vacuum. Heavy write tables—carts, audit logs, notification queues—need aggressive autovacuum.
ALTER TABLE orders SET (
autovacuum_vacuum_scale_factor = 0.05,
autovacuum_analyze_scale_factor = 0.02
); Check bloat with pg_stat_user_tables:
SELECT relname, n_live_tup, n_dead_tup,
round(100.0 * n_dead_tup / NULLIF(n_live_tup + n_dead_tup, 0), 2) AS dead_pct
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 15; Indexes and EXPLAIN
Run EXPLAIN (ANALYZE, BUFFERS) on slow Eloquent queries. Partial indexes help soft-deleted models:
CREATE INDEX idx_orders_open ON orders (created_at)
WHERE deleted_at IS NULL; For JSON columns, GIN indexes beat sequential scans. Compare patterns in MySQL 8 vs PostgreSQL for web apps if your team is split across engines.
Redis 8.10 for cache and session storage offloads read pressure. PostgreSQL should not cache what Redis handles better. On eCommerce projects like Quick And Easy Nepalese Grocery, delivery-zone lookups need indexed postcode columns—not full table scans at checkout.
How do you monitor PostgreSQL and handle common production problems?
Monitoring turns admin from reactive firefighting into scheduled work. You need metrics, logs, and alert thresholds.
Essential queries every admin should bookmark
Active connections and idle clients:
SELECT state, count(*) FROM pg_stat_activity GROUP BY state; Long-running queries:
SELECT pid, now() - query_start AS duration, query
FROM pg_stat_activity
WHERE state = 'active' AND query_start < now() - interval '30 seconds'; Table and index size:
SELECT relname, pg_size_pretty(pg_total_relation_size(relid))
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC
LIMIT 10; Export these to Grafana via postgres_exporter, or run them from a cron script that emails on threshold breach. For replication setups, see PostgreSQL replication and high availability.
Common failures and fixes
- Disk full on WAL volume — expand disk, then run
SELECT pg_switch_wal();and verify archiving resumes - Too many connections — add PgBouncer in transaction pooling mode; lower Laravel
DB_PERSISTENTmisuse - Lock waits on deploy — use
lock_timeoutin migration sessions; schedule DDL off-peak - Sequence exhaustion on bigserial — rare but real; monitor
pg_sequenceson high-insert tables - Permission denied after restore — re-run grants; pg_dump -Fc with
--no-ownerneeds explicit role mapping
Terminal skills from essential Ubuntu terminal commands help during SSH sessions at 2 AM. Keep a runbook doc next to the server IP list.
Structured logs help API teams debug JSON payloads. Paste sanitized query output through a JSON formatter before sharing in tickets. For ongoing DBA work you cannot staff in-house, support and maintenance covers backup verification and upgrade planning.
Enterprise apps with strict uptime targets should pair monitoring with HA architecture. Enterprise application development projects I have shipped treat the database as a first-class component—not an afterthought behind PHP code.
The official monitoring chapter in the PostgreSQL documentation lists every pg_stat_* view. Bookmark it. pg_dump reference documents format flags that matter for large restores.
If your stack also runs document stores, compare routines with MongoDB administration basics. Different engine, same discipline: backups, monitoring, rehearsed recovery.
Key Takeaways
- Install PostgreSQL 18 with scoped roles, pg_hba.conf restrictions, and TLS before you point production Laravel traffic at it.
- Automate pg_dump nightly and enable WAL archiving if you need point-in-time recovery—then prove restores work on staging.
- Tune shared_buffers and autovacuum first; fix application N+1 queries and missing indexes before chasing exotic parameters.
- Monitor connections, dead tuples, disk usage, and slow queries with pg_stat views or postgres_exporter alerts.
- Keep a troubleshooting runbook for disk-full WAL, connection exhaustion, and migration lock waits.
- Rehearse major version upgrades on a clone; PostgreSQL 17 to 18 jumps need extension compatibility checks.
People Also Ask
Is PostgreSQL hard to administer compared to MySQL?
PostgreSQL asks for more upfront tuning and vacuum awareness. MySQL feels simpler on cPanel-style hosting. PostgreSQL gives you stricter SQL, better concurrency, and richer indexing. Teams with a dedicated admin—or a managed service— rarely regret the choice for complex apps.
How often should you vacuum PostgreSQL?
Autovacuum runs continuously by default. You intervene when n_dead_tup stays high on hot tables. Schedule manual VACUUM (ANALYZE) after large bulk imports or data purges. Never disable autovacuum globally.
What is the best backup strategy for PostgreSQL in 2026?
Combine nightly pg_dump custom-format files stored off-server with weekly pg_basebackup plus continuous WAL archiving for PITR. Match retention to compliance needs. Test a full restore every month.
Do Laravel apps need a connection pooler for PostgreSQL?
Once you run more than a few PHP-FPM workers, yes. Each worker can hold idle connections. PgBouncer in transaction pooling mode keeps PostgreSQL max_connections sane without rewriting Eloquent.
Ship PostgreSQL with confidence
PostgreSQL Administration Essentials is not glamorous work. It is what separates a demo database from one that survives bad deploys, holiday traffic, and disk surprises. Install cleanly, back up provably, tune with data, and monitor before users notice. If your team needs hands-on help with PostgreSQL on Ubuntu—or a full Laravel stack built around it—contact us to discuss your project. You can also browse the home page for related guides, or read more about my background on about me and API development work with PostgreSQL backends.
Frequently Asked Questions
0 Comments
Leave a comment
Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

