escamil - Hugo deployed website

Rob's Menagerie (recipes and such)

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)

  1. in AWS EC2 Console, click “Launch Instance” button
  2. Add Name, select Amazon Linux
  3. provide key pair name - or create new pair if-needed (chmod 400 if newly created)
  4. 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
  5. Allow SSH traffic from “My IP” in the drop-down. (optional, but recommended)
  6. Launch Instance in lower right button

Deploy Postgres-17 install and expose ports

  1. Connect to your EC2 Instance
    • ssh -i {pem file} ec2-user@{ec2 DNS name}
  2. Create prerequisite postgres directories
    • sudo mkdir /var/lib/pgsql/
    • sudo chown postgres:postgres /var/lib/pgsql/
  3. Identify latest supported version of PostgreSQL:
    • sudo yum search "postgres"
  4. Install postgres software
    • sudo yum install postgresql17-server.x86_64
  5. Initialize database:
    • sudo /usr/bin/postgresql-setup --initdb
  6. Use Amazon Linux commands to start/enable datbaase:
    • sudo systemctl start postgresql
    • sudo systemctl enable postgresql
    • sudo systemctl status postgresql
  7. Create credentials for initialized database:
    • sudo su - postgres
    • psql -c "ALTER USER postgres WITH PASSWORD '{new_password}';"

Update listener/postmaster to accept external connections

  1. 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
  2. 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.

Last updated on 29 Apr 2025
Published on 29 Apr 2025