Amazon EC2
Installing PostgreSQL using Amazon EC2 and Amazon Linux
Strictly for demonstration purposes, an optimal configuration would be RDS/Aurora instance(s).
Review content in AWS RDS walkthru for comparison and cost information
Time requirement:
~30 mins
Prerequisite:
VPC with public subnet.
Knowing your IP Address to create security group with limited access
Create EC2 Instance using latest AMI and preferred hardware type (t2.micro suitable for demo)
- in AWS EC2 Console, click “Launch Instance” button
- Add Name, select Amazon Linux
- provide key pair name - or create new pair if-needed (chmod 400 if newly created)
- Select Network Settings to pre-created VPC and Public Subnet
- verify “Auto-assign public IP” is “Enabled”
- if option is unavailable, VPC does not have public subnet
- Allow SSH traffic from “My IP” in the drop-down. (optional, but recommended)
- Launch Instance in lower right button
Deploy Postgres-17 install and expose ports
- Connect to your EC2 Instance
- ssh -i {pem file} ec2-user@{ec2 DNS name}
- Create prerequisite postgres directories
sudo mkdir /var/lib/pgsql/
sudo chown postgres:postgres /var/lib/pgsql/
- Identify latest supported version of PostgreSQL:
sudo yum search "postgres"
- Install postgres software
sudo yum install postgresql17-server.x86_64
- Initialize database:
sudo /usr/bin/postgresql-setup --initdb
- Use Amazon Linux commands to start/enable datbaase:
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql
- Create credentials for initialized database:
sudo su - postgres
psql -c "ALTER USER postgres WITH PASSWORD '{new_password}';"
Update listener/postmaster to accept external connections
- Update default listen_address from localhost to accept IPv4 connections:
vi /var/lib/pgsql/data/postgresql.conf
- update listen_addresses = ’localhost’ to *
vi /var/lib/pgsql/data/pg_hba.conf
- update ipV4 to 0.0.0.0/0 md5
- Restart postgres to process updated files:
sudo systemctl restart postgresql
If connections fail, verify VPC security groups
- Select the security group associated with your newly created VPC most likely the last, or longest-named group ID
- Edit inbound rules to add Postgres port 5432
- Click “Add Rule” > Type > PostgreSQL > Source: My IP
Route table for the VPC may also require whitelisting port 5432
Connection and further maintenance
In my example, I was provided the endpoint used to connect as:
psql -h database-1.cvf3het7fmyg.us-west-1.rds.amazonaws.com -U postgres
Cleanup EC2 resources
Using EC2 console, check running instance name and use “Instance state” > Terminate
Termination may take several minutes.