Connect to an AWS RDS PostgreSQL database using PSQL

EmilioEmilio
3 min read

Here at Emilio we decided to go for Postgres. I will assume you already have a Postgres instance in AWS RDS. I will create an article on how to do this and link it here when I have it ready.

Get the endpoint and port of your database

  1. Go to https://console.aws.amazon.com/rds/

  2. Select the correct region at the top right of the window

  3. On the left panel click on “Databases”

  4. Enter the Postgres database by clicking on its identifier

  5. Under the “Connectivity & security” pane, copy the endpoint and port

Get the VPC your database lives under

In the same window, under “Connectivity & security”, under “Networking”, note down the VPC the database lives in, you will need it for the next step.

Create or update a security group

To be able to access the database, you will need to allow incoming traffic from wherever you’re connecting from. In my case it will be my home computer, so I will need to allow my home IP address.

To do this:

  1. Go to https://console.aws.amazon.com/ec2

  2. In the left panel, under “Network & Security”, click on “Security Groups”

  3. You can modify an existing one or create one by hitting “Create Security Group” at the top right of the window

  4. Fill in the basic details (select the VPC you noted down in the previous step):

5. Under “Inbound Rules” select all traffic and where it says source click on “My IP”

This will set automatically your location’s public IP. If you want to set a specific IP yous leave it on “Custom” and start typing your IP in the search box.

Remember it should finish in a subnet mask “/24” or “/32” for instance. You can learn more about subnet masks here.

6. Add any other inbound rules you want, leave the outbound rule as is and save the security group.

7. Go back to your RDS Postgres instance, click on Modify at the top right.

8. Scroll down until Connectivity and add the security group you just created.

9. Save!

Ok so now everything’s ready for you to connect. You just need the PSQL client.

Set up PSQL

I use WSL and will explain this for Linux (I freaking love Ubuntu) but you could also install it on Mac and Windows. Check the specific process here.

1. Install it using apt

sudo apt install postgresql-client

2. Check the installation was successful by

psql --version

Connect!

Use the following command with the data you noted down from your RDS Postgres instance:

psql -h <db-endpoint> -p <port-number> -d <db-name> -U <username> -W
  • -W prompts for a password even if you don’t have one, remove this if not needed
0
Subscribe to my newsletter

Read articles from Emilio directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Emilio
Emilio