PostgreSQL Production-Ready Installation Script

This script will:
- Install PostgreSQL
- Start and enable PostgreSQL service
- Create a database
- Create a database user
- Enable remote access
- Open port 5432 in UFW firewall
- Generate a FastAPI-ready connection string
1. Create the Script
nano postgresql_setup.sh
Ready to Transform Your Business?
Paste the following code:
#!/bin/bash
set -e
# ----------------------------
# Configuration
# ----------------------------
DB_NAME="Your_DB_Name"
DB_USER="Your_ DB_USER"
DB_PASS="Your_DB_PASS"
echo "========================================"
echo "Updating System"
echo "========================================"
apt update
apt upgrade -y
echo "========================================"
echo "Installing PostgreSQL"
echo "========================================"
apt install -y postgresql postgresql-contrib postgresql-client ufw
echo "========================================"
echo "Starting PostgreSQL"
echo "========================================"
systemctl enable postgresql
systemctl start postgresql
VERSION=$(ls /etc/postgresql | sort -V | tail -n1)
echo "Detected PostgreSQL Version: $VERSION"
echo "========================================"
echo "Creating Database User"
echo "========================================"
sudo -u postgres psql <<EOF
DO
\$do\$
BEGIN
IF NOT EXISTS (
SELECT FROM pg_catalog.pg_roles
WHERE rolname = '$DB_USER') THEN
CREATE ROLE $DB_USER LOGIN PASSWORD '$DB_PASS';
END IF;
END
\$do\$;
EOF
echo "========================================"
echo "Creating Database"
echo "========================================"
DB_EXISTS=$(sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'")
if [ "$DB_EXISTS" != "1" ]; then
sudo -u postgres createdb -O $DB_USER $DB_NAME
fi
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
echo "========================================"
echo "Enabling Remote Connections"
echo "========================================"
CONF="/etc/postgresql/$VERSION/main/postgresql.conf"
HBA="/etc/postgresql/$VERSION/main/pg_hba.conf"
sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" $CONF
sed -i "s/^listen_addresses = 'localhost'/listen_addresses = '*'/" $CONF
grep -q "0.0.0.0/0" $HBA || echo "host all all 0.0.0.0/0 md5" >> $HBA
echo "========================================"
echo "Restarting PostgreSQL"
echo "========================================"
systemctl restart postgresql
echo "========================================"
echo "Configuring Firewall"
echo "========================================"
ufw allow OpenSSH || true
ufw allow 5432/tcp || true
ufw --force enable
SERVER_IP=$(curl -4 -s ifconfig.me || echo "YOUR_SERVER_IP")
echo ""
echo "========================================"
echo "INSTALLATION COMPLETE"
echo "========================================"
echo ""
echo "Database : $DB_NAME"
echo "Username : $DB_USER"
echo "Password : $DB_PASS"
echo ""
echo "FastAPI DATABASE_URL:"
echo ""
echo "postgresql+psycopg://$DB_USER:$DB_PASS@$SERVER_IP:5432/$DB_NAME"
echo ""
echo "Check Version:"
echo "psql --version"
echo ""
echo "Login:"
echo "sudo -u postgres psql"
echo ""
echo "Test Connection:"
echo "psql -h localhost -U $DB_USER -d $DB_NAME"
echo ""
echo "List Databases:"
echo "sudo -u postgres psql -c \"\\l\""
echo ""
echo "List Users:"
echo "sudo -u postgres psql -c \"\\du\""
echo ""2. Make the Script Executable
chmod +x postgresql_setup.sh
3. Run the Script
sudo ./postgresql_setup.sh
Turn Ideas Into Business Growth
4. Verify PostgreSQL Installation
Check the installed version:
psql --version
Check PostgreSQL cluster status:
pg_lsclusters
Expected output:
Ver Cluster Port Status Owner
16 main 5432 online postgres
5. Test Database Login
psql -h localhost -U DB_USER -d DB_Name
Enter the password:
DB_Password
6. FastAPI Environment Variable
Add the following to your .env file:
DATABASE_URL=postgresql+psycopg://db_user:db_password@YOUR_SERVER_IP:5432/db_name
7. Production Security Recommendation
For testing, the script allows connections from anywhere:
host all all 0.0.0.0/0 md5
For production environments, replace it with your specific public IP address:
host all all YOUR_PUBLIC_IP/32 md5
Examples:
host all all 203.0.113.45/32 md5
Thank You!
