Spring Boot HandBook

     Setup Production Database with RDS in AWS 

    Introduction to AWS RDS#

    Amazon RDS (Relational Database Service) is a managed service that makes it easy to set up, operate, and scale relational databases in the cloud. It automates tasks such as backups, patching, and scaling, allowing developers to focus on building applications instead of managing infrastructure.

    With RDS, you can deploy popular database engines like MySQL, PostgreSQL, MariaDB, Oracle, and SQL Server. It provides high availability, security, and performance for production-grade databases.

    Lets us see the steps to setup our postgresql with RDS#

    Step 1#

    Login to your AWS account first and after login click on services and search for RDS service

    AWS RDS service

    Step 2#

    You will see the below page click on create database button.

    Creating Relation database in AWS RDS

    Step 3#

    In next step you will be redirected to create DB page, here select standard create option and select postgresql database.

    Create Database Page

    Step 4#

    Scroll down, you will see engine version select the required one, we will select the latest version. In Template section select free tier if you are free tier aws user.

    Selecting Postgres version in RDS

    Step 5#

    Scroll down and you will see the settings section for your Database.

    Here you have to set the database instance name, we have set the name as my-postgres-db

    Next in credentials settings you have to set the username and password for your database instance, In credential management option select self managed if you want to manage the credentials by your own.

    Setting postgres DB credentials in AWS RDS

    Step 6#

    Next Scroll down and you will see the instance configuration section.

    From here select Database type, we will select t3 micro as we are free tier user you can choose other db type like large xlarge as per requirement.

    Next in Storage section we will select storage type as SSD gp2 you can choose other also and we will allocate 20 GiB storage to our DB.

    Setting DB instance configuration

    In storage section we also have storage autoscaling option. Click on check box if you want to auto scale storage if demand increases.

    Setting DB storage and autoscaling

    Step 7#

    Scroll down and now you will see the connectivity section for your database.

    In compute resource we will select don’t connect to an EC2 compute resource, but if you want your DB to be connected to ec2 instance then select connect to EC2 compute resource and as you click on it you will see the option to select which instances you want to connect.

    In VPC select default VPC.

    In DB subnet group select the default vpc

    Select yes for public access.

    Setting DB connectivity 

    After that we have to assign security group also, which will have allow us to define rules like which IP addresses are allowed to access our Database.

    You can create a new security group or choose from existing ones. We will create a new security group with name postgres-spring-sg.

    In Availability zone select the availability zone you want where the replica of your DB will be store so that on DB downtime you can backup from the other selected availability zones.

    Keep certificate authority as default.

    And in additional configuration set database port 5432

    Step 8#

    Scroll down you will see next Tags we will not create any tags, authentication keep it as password auhentication, Monitoring keep it unchecked for now.

    Choosing Database authentication method

    Step 9#

    Scroll down you will see additional configuration section

    You wil see initial db name if you want initial db to be created then you can give the name for it, we have given test db name.

    And rest all settings (Backup, encryption, Maintenance) keep it as default. and click on create Database button.

    Setting additional configuration
    Setting encryption to default 
    Create RDS Postgres database

    Note:- If you see any pop-ups for add-ons skip it as it can cost you in free tier.

    Step 10#

    Now go to RDS service and select your postgres-spring DB instance and go to its connectivity and security section and click on the security group.

    We will define rules in security group so that we can access our postgres DB from workbench or DBeaver.

    RDS Postgres Database instance

    Click on the security group

    Security group of Postgres RDS instance

    Click on edit inbound rules.

    Security group of Postgres RDS instance

    Now you can set the ip addresses which can access your Database, we will select anywhere so that anyone can access it from anywhere although not recommended you can set only for your ip address by selecting My IP option from dropdown.

    Editing Security inboud rules

    Step 11#

    Now copy the endpoint to access your database. and connect it from workbench we will use DBevear to connect our postgres-spring database.

    1. Open DBevear or any other software from where you want to connect the Database.
    2. Open DBever and create new connection.

    keep databse name as test as we have added initial db name as test during setup in RDS. Keep port as 5432 as we have added port in configuration as 5432 during configuration.

    Enter username and password that you have given during configuring postgres-spring DB instance.

    Connecting AWS Postgres database in dbeaver 

    After clicking on Test connection button you will see that connection is successfull.

    Connecting AWS Postgres database in dbeaver 

    Now you can see your test database in database navigator.

    Connected to AWS postgres test database in dbeaver 

    In this article, we explored how to set up a PostgreSQL database using Amazon RDS. By following the outlined steps, you should now have a fully operational RDS instance that allows you to leverage the advantages of a managed relational database service. With RDS, you can focus on developing your applications while AWS handles the heavy lifting of database management. Whether you're a beginner or an experienced developer, AWS RDS provides the scalability, reliability, and security needed for your database needs.

    Last updated on Dec 27, 2024