Setting Up A PostgreSQL Database on AWS: A Step-by-Step Guide to Generate a Service Connection String
Tags: aws, postgres, database, cloud
Published: 2024-01-11 19:50:34

If you're reading this, you've probably struggled with how to set up a Postgres database on AWS and generate a connection for usage in your application. 

Perhaps you are like me, highly focused on backend development, and need to add the connection in your `.env` file. You're probably under a lot of mental strain because you just built or are about to build a $16 per month database instance and AWS wants you to utilize 101% of your brain, making it difficult to connect to the database instance.

This article provides a step-by-step guide on how to create, configure, and connect to your database instance on AWS. The knowledge provided can be applied to a wide range of scenarios.

## Prerequisite

This guide will walk you through the steps of creating a free database instance on AWS. However, it is important to note that you will incur charges for using it, so it's advisable to delete the instance when you are not actively using it.

If you already know how to create and configure an instance, skip to the `connection string generation` section for help with connection.

All highlighted fields and sections in the images of this article are considered important to take note of.

## Let's Get Started

First, navigate to Amazon AWS and create/log in to your account.

It can be done using the following link : [Amazon AWS](https://aws.amazon.com/)

![Amazon AWS Landing Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2udjr7wakuxylai8qcyc.png)

### Navigate To The Resources For Creating A Database 

In the console's home menu, we select the 'RDS' section as it manages resources and instances for relational databases. The abbreviation stands for 'Relational Database Service'.

![RDS Selection Field](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a73mtit2q7zuoftns38r.png)


### Select A Creation Method

The `selection method` is choosing between setting up your instance yourself or letting Amazon's service take the wheel for you.

For this tutorial, we will select the `standard create` option as it provides more flexibility to customize.

![selection method fields](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oe904l39hs9lt8rpsfg.png)

### Database Engine Type Selection

In this article, we will be using Postgres as our database engine, so naturally, it should be our top choice.

![postgres engineer selected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txjym9ojxdz84zoeo0qw.png)

After selecting the engine type, we will choose a free tier template for the tutorial. This will also prevent us from getting charged heavily in case we forget to delete the resources.

![free tier selection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ianazv9w2c6ybf32g7dw.png)

## Important Database Setting

#### Name The DB instance identifier

It is important to note that adding a database instance identifier that is descriptive is very crucial to identify instance and it should be done for your ease

![descriptive instance identifier](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2nccl5ui0tb7c8r1s8d.png)

#### Credential Settings

This simply involves adding the database username and password that should be tied to the instance on creation

![credenctial setting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjujvubxqx0413ihxv0k.png)

_It is essential to create a unique password for the database instance instead of relying on the auto-generated password field for easy recall._

### Database Instance Configuration

For the final step of the first phase (creation of Database instance), we will add the most important.

You need to scroll down until you see a section labeled `Additional configuration`.

This section holds the field that will be used to name our database and it is very important because without it there will be no database created and this part is missed a lot.

![naming the database resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftnh6fkht2r8i393t4fr.png)

Once the entire configuration is complete, your billing should match this if you have done everything correctly.

![billing for database instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiprq2pp3tua5b40ehzo.png)

Then proceed to hit the create database button. 

It will take approximately 5-10 minutes or less to create. Once completed, we can proceed to the next phase.

### Connection String Generation
This is the second phase, and I must acknowledge that it is the trickiest part of it all.

The format for putting together an Amazon AWS Postgres database connection string together is simply :

```
postgres://<username>:<password>@<database_endpoint>/<database_name>
```

- username = `master username`
- password = `initially added password`
- database_endpoint = `endpoint generated by AWS`
- database_name = `name given to database on creation`

To begin with, please navigate to the newly created resources. If you're already there, scroll down a bit and you'll find the database endpoint.

This is a crucial component in generating the connection string.

![database instance endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzbcv3js0oqonf4kdc7p.png)

Afterward, click on the `Configuration` section in the newly created instance

![configution section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pvecku7pg292qzsk2qm.png)

Get the username, password, and database name from the `Configuration` section when you scroll slightly downward

![configuration information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cy75v5asvtyoynon5l8t.png)

Now that we have constructed the connection string, can we start working on the database?

"The answer is no."

Before putting the database instance to good use, one additional configuration must be done for full remote access.

### Access Configuration

This involves declaring access to the database instance as part of a security measure to prevent unwanted connections and access to resources.

To accomplish this, you will need to add an inbound rule to the database instance.

In the `Connectivity & security` section, we will click on the link in the `VPC security groups` field

![security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7n5fqy9gk2rlejnrjb5.png)

Let's navigate to the `Security group ID` of the database instance to make modifications.

![security group ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/clct06cq4o68wg3ixkfo.png)

Click the `edit inbound rules` button

![edit inbound rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxg2vc0hme2hqahuf1yf.png)

Click on `Add rules` below the already predefined rules and add two rules

![adding rules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/304gxs7wykyppebugdtc.png)

- highlighted red = `protocol type`
- highlighted blue = `the database port [which by default is "5432"]` 
- highlighted cyan = `connection source`

Save these rules put in place and configured. You are good to connect to your database on Amazon RDS and have fun with it.

Note that the **highlighted green** indicates a security warning. If possible, please provide the IP address of your service instance or local machine to avoid this security flag that might eventually turn into a security vulnerability.

## Conclusion

with this guide, you should be able to set up a Postgres database on Amazon AWS, configure and have it connected to your local machine or web server using the connection string.

I hope this article was insightful and that you find It helpful 

Pro tip: Avoid unnecessary charges by shutting down and deleting unused AWS instances. 

Thank you for reading, drop a comment if you have a question, and have fun :)