Install PostgreSQL on Ubuntu, create your first database and user, configure remote access, and secure it for production use.
In this guide
Run: sudo apt update && sudo apt install -y postgresql postgresql-contrib. PostgreSQL starts automatically. Check status: sudo systemctl status postgresql. The installation creates a postgres Linux user and a postgres database superuser. Connect: sudo -u postgres psql. You are now in the PostgreSQL shell.
In the psql shell: CREATE USER myuser WITH PASSWORD 'strongpassword'; CREATE DATABASE mydb OWNER myuser; GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser; \q. Never use the postgres superuser for your application. Create a dedicated user with only the permissions it needs — principle of least privilege.
By default PostgreSQL only accepts local connections. Edit /etc/postgresql/*/main/postgresql.conf: set listen_addresses = '*' (or specific IPs). Edit /etc/postgresql/*/main/pg_hba.conf: add a line for your app server IP: host mydb myuser app_server_ip/32 scram-sha-256. Restart: sudo systemctl restart postgresql. Only open to IPs that need access — never expose PostgreSQL port 5432 to the internet.
From your application server, test: psql -h db_server_ip -U myuser -d mydb. If it connects, your app can use the connection string: postgresql://myuser:password@db_server_ip:5432/mydb. Store this in your .env file as DATABASE_URL. Make sure your server firewall (UFW or AWS Security Group) allows TCP port 5432 from your app server's IP.
Create a backup script at /home/ubuntu/backup_db.sh: #!/bin/bash — pg_dump -U postgres mydb | gzip > /backups/mydb_$(date +%Y%m%d).sql.gz. Make it executable: chmod +x backup_db.sh. Schedule it with cron: crontab -e — add: 0 2 * * * /home/ubuntu/backup_db.sh. This runs a compressed backup at 2am daily. Copy backups to S3 or another server for offsite redundancy.
Need Help?
Our engineering team handles implementations like this every week. Get a free scoping call — we will tell you exactly what it takes and what it costs.
Book a free callCompetitive Intelligence
Efficiency Modeling
© 2026 NexWorldTech — Built for Global Dominance.