- An Amazon Linux 2023 instance
- Sudo privileges
- Active internet connection
sudo dnf update -y
# Install the postgresql-server package
sudo dnf install -y postgresql15-server postgresql15
# Initialize the database
sudo postgresql-15-setup initdb
# Start PostgreSQL service
sudo systemctl start postgresql-15
# Enable PostgreSQL to start on boot
sudo systemctl enable postgresql-15
# Verify the service status
sudo systemctl status postgresql-15
# Backup the original configuration
sudo cp /var/lib/pgsql/15/data/postgresql.conf /var/lib/pgsql/15/data/postgresql.conf.bak
sudo cp /var/lib/pgsql/15/data/pg_hba.conf /var/lib/pgsql/15/data/pg_hba.conf.bak
# Edit postgresql.conf to allow remote connections
sudo vi /var/lib/pgsql/15/data/postgresql.conf
Add or modify these lines:
listen_addresses = '*' # Listen on all available IP addresses
port = 5432 # Default port
# Edit pg_hba.conf
sudo vi /var/lib/pgsql/15/data/pg_hba.conf
Add these lines for remote access (adjust according to your security needs):
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# Remote connections - replace with your specific IP range
host all all 0.0.0.0/0 scram-sha-256
sudo systemctl restart postgresql-15
# Switch to postgres user
sudo -u postgres psql
# In PostgreSQL prompt, set password
ALTER USER postgres WITH PASSWORD 'your_secure_password';
# Exit PostgreSQL prompt
\q
# Allow PostgreSQL port through firewall
sudo firewall-cmd --permanent --add-port=5432/tcp
sudo firewall-cmd --reload
psql --version
# Connect to PostgreSQL
sudo -u postgres psql
# Check connection info
\conninfo
# Exit
\q
- Always use strong passwords
- Restrict remote access to specific IP addresses when possible
- Keep the system and PostgreSQL updated
- Regular backup of database
- Monitor logs for suspicious activities
Edit /var/lib/pgsql/15/data/postgresql.conf
:
# Memory Configuration
shared_buffers = 256MB # 25% of RAM for dedicated server
work_mem = 4MB # Depends on workload
maintenance_work_mem = 64MB # For maintenance operations
# Checkpoint Configuration
checkpoint_completion_target = 0.9
wal_buffers = 16MB
# Query Planner Configuration
effective_cache_size = 768MB # 75% of RAM for dedicated server
log_destination = 'csvlog'
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 10MB
log_min_duration_statement = 1000 # Log queries taking more than 1 second