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
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'.
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.
Database Engine Type Selection
In this article, we will be using Postgres as our database engine, so naturally, it should be our top choice.
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.
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
Credential Settings
This simply involves adding the database username and password that should be tied to the instance on creation
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.
Once the entire configuration is complete, your billing should match this if you have done everything correctly.
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.
Afterward, click on the Configuration
section in the newly created instance
Get the username, password, and database name from the Configuration
section when you scroll slightly downward
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
Let's navigate to the Security group ID
of the database instance to make modifications.
Click the edit inbound rules
button
Click on Add rules
below the already predefined rules and add two rules
- 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 :)
Top comments (0)