Skip to main content

Connecting to live databases

⚠ī¸ Warning: Particularly if you're connecting to a production database, proceed with caution and if possible, look to pair with someone else and mark the connection read-only.

Using details in AWS and a database client, you can connect via a bastion host to the PostgreSQL database for the application from your local machine.

Prerequisites#

Environments#

EnvironmentAWS accountKeyEC2 bastion
TestingSocial-Care-Workflows-Stagingdevsocial-care-workflows-dev-bastion
StagingSocial-Care-Workflows-Stagingstgsocial-care-workflows-stg-bastion
ProductionSocial-Care-Workflows-Productionprodsocial-care-workflows-prod-bastion

Connecting to the PostgreSQL (RDS PostgreSQL) database#

ℹī¸ Information: The steps and terminology for each database client will be slightly different, but they'll all ask for the same information.

  1. Open your database client and add a new data source for a PostgreSQL database
  2. Name your database source e.g. Workflows (Staging)

Within the relevant AWS account:

  1. Go to EC2 → Instances
  2. Click on your relevant EC2 bastion, see environments
  3. Under the Details tab and Instance summary section, copy the Public IPv4 DNS

Back within your database client:

  1. Create a SSH tunnel and set the copied value for Public IPv4 DNS as the host and ec2-user as the username

Back in AWS:

  1. Go to Secrets Manager
  2. Search for social-care-workflows_< environment-key >_private_key, replacing < environment-key > with the key for the environment e.g. social-care-workflows_stg_private_key, see environments
  3. Click on the social-care-workflows_< environment-key >_private_key secret
  4. Under Secret value, click on Retrieve secret value
  5. Copy the private key

On your local machine:

  1. Create a new .pem file in your ~/.ssh folder e.g. social-care-workflows-staging.pem with the private key

Back within your database client:

  1. Select authentication type as "key pair" or similar
  2. Select the path of the private key you just created as the private key file for the SSH tunnel

Back in AWS:

  1. Go to Systems Manager → Parameter Store
  2. Search for social-care-workflows-< environment-key >/database/url, replacing < environment-key > with the key for the environment, see environments
  3. Click on the social-care-workflows-< environment-key >/database/url parameter store value
  4. Click on Show for the Value

Back within your database client:

  1. Under "General" or similar, fill in the values for host, port, user, password and database by extracting parts of the database URL
postgres://<USER>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
  1. Test and create the data source

💡 Hint: Some database clients allow you to set a data source as read-only, this is to prevent accidentally making changes on a database you didn't mean to. Further to this, if helpful for you, some clients also allow you to customise the colour of each connection, so you can more easily differentiate them. If possible, set these for production.

Tables live under the public schema, see below for an example query:

SELECT * FROM public."Workflow" LIMIT 10;