In this we are going to cover How to Create PostgreSQL RDS in AWS | Connect PostgreSQL RDS using EC2 Instance.
Table of Contents
What is RDS is AWS ?
Amazon RDS (Relational Database Service) is a managed database service provided by Amazon Web Services (AWS) that simplifies the setup, operation, and scaling of relational databases. RDS takes care of routine database tasks such as provisioning, patching, backup, recovery, and scaling, allowing you to focus on building your applications rather than managing database infrastructure.
Here are some key features and benefits of Amazon RDS:
- Managed Service: AWS takes care of the administrative tasks such as database setup, patching, backups, and maintenance, allowing developers to focus on their applications rather than database management.
- Multiple Database Engines: RDS supports several popular relational database engines, including MySQL, PostgreSQL, MariaDB, Oracle, and Microsoft SQL Server. This gives you the flexibility to choose the database engine that best fits your application’s needs.
- Automated Backups: RDS automatically creates daily backups of your database and retains them for a specified retention period, making it easy to recover from data loss or corruption.
- High Availability: RDS offers options for high availability, including Multi-AZ (Availability Zone) deployments, which provide failover capabilities and automatic data replication to a standby instance in a different Availability Zone for enhanced reliability.
- Scalability: You can easily scale your RDS instance vertically (by resizing) or horizontally (by using Read Replicas) to accommodate changes in traffic and workload.
- Security: RDS provides security features like encryption at rest and in transit, database instance isolation, and integration with AWS Identity and Access Management (IAM) for fine-grained access control.
- Monitoring and Metrics: AWS CloudWatch can be used to monitor RDS instances and collect metrics for performance analysis. You can set up alarms and notifications based on these metrics.
- Automated Software Patching: RDS can automatically apply minor and major database engine version upgrades, reducing the burden of manual updates and ensuring that your database remains up to date and secure.
- Scalable Storage: You can easily adjust the storage capacity of your RDS instance to meet the growing storage needs of your application.
- Global Databases: RDS provides the capability to create read replicas in different regions, allowing you to deploy globally distributed applications with low-latency access to your data.
Amazon RDS simplifies database management in AWS and is a popular choice for businesses and developers looking to run relational databases in a cloud-native environment while benefiting from AWS’s infrastructure and management capabilities.
What is PostgreSQL ?
PostgreSQL, often referred to simply as “Postgres,” is an open-source relational database management system (RDBMS). It is known for its reliability, robustness, and extensive features, making it a popular choice for a wide range of applications, from small projects to large-scale enterprise systems. It is widely used as a database system for various types of applications, including web applications, enterprise software, and data warehousing.
Here are some key features and characteristics of PostgreSQL:
- Open Source: PostgreSQL is open-source software, which means it is freely available for anyone to use, modify, and distribute.
- Relational Database: It is a relational database system, meaning it stores data in tables with rows and columns and supports SQL (Structured Query Language) for data manipulation.
- ACID Compliance: PostgreSQL adheres to the ACID (Atomicity, Consistency, Isolation, Durability) properties, ensuring data consistency and reliability even in the face of hardware failures or crashes.
- Extensible: PostgreSQL allows users to define custom data types, operators, functions, and even create their own extensions, enabling advanced data modeling and customizations.
- Multi-Version Concurrency Control (MVCC): MVCC is a technique that allows multiple transactions to occur simultaneously without blocking each other, providing high concurrency and data consistency.
- Advanced Data Types: PostgreSQL supports a wide range of data types, including JSON, XML, hstore (key-value storage), and more.
- Full-Text Search: It offers powerful full-text search capabilities, making it suitable for applications with search requirements.
- Geospatial Capabilities: PostgreSQL includes support for geospatial data and provides features for location-based applications.
- Extensive Extensions: The PostgreSQL community maintains a rich ecosystem of extensions that can enhance functionality, such as PostGIS for geospatial data or pgcrypto for cryptographic operations.
- Replication and High Availability: PostgreSQL supports various replication methods and can be configured for high availability setups, including master-slave replication and failover.
- Community Support: PostgreSQL has an active and vibrant community of developers and users, offering support through mailing lists, forums, and online resources.
- Cross-Platform: It is available on multiple operating systems, including Linux, Windows, macOS, and more.
- Scalability: PostgreSQL can scale both vertically (by increasing server resources) and horizontally (by using partitioning and sharding techniques).
- Security: It provides features like role-based access control (RBAC), SSL/TLS encryption, and authentication mechanisms for securing data.
- Foreign Data Wrappers (FDW): FDW allows PostgreSQL to connect and query data from remote data sources, enabling integration with other databases and systems.
- PostgreSQL is a versatile database system used in a wide range of applications, including web and mobile apps, data warehousing, geospatial applications, and more. Its flexibility, performance, and strong community support make it a reliable choice for many database needs.
How to create PostgreSQL RDS in AWS
Sign in to AWS Console: Log in to your AWS Management Console using your credentials.
Navigate to RDS: From the AWS Management Console, navigate to the “RDS” service.
Click “Create Database”: Click on the “Create Database” button to start the process of creating a new RDS instance.
Choose a Database Creation Method: You’ll be presented with two options – “Standard create” and “Easy create”. Choose “Standard create” for more configuration options.
In the “Engine options” section, Select “PostgreSQL” as the database engine. Choose the PostgreSQL version you want to use.
Choose Use Case: Select the use case that best fits your needs (e.g., Dev/Test, Production).Choose the “Free tier” template if you’re eligible for the AWS Free Tier. Otherwise, choose a template that suits your needs in terms of performance and capacity.
Specify DB Details: Fill in the necessary details for your PostgreSQL RDS instance, including: Instance specifications (DB instance class, storage, etc.).
DB instance identifier: A unique name for your RDS instance.
Master username: The username for the master user of the database.
Master password: The password for the master user.
Confirm password: Confirm the password.
DB Instance Size:
Choose an appropriate instance size based on your performance requirements. If you’re eligible for the Free Tier, make sure to select an instance size that falls within the Free Tier limits.
Storage: Specify the amount of storage you need for your instance.
Settings for DB instance identifier, master username, and password.
Public accessibility options.
Configure the Virtual Private Cloud (VPC) settings.
Choose a subnet group.
Choose an existing security group or create a new one to control inbound and outbound traffic to the RDS instance.
Virtual Private Cloud (VPC) settings.
Database Authentication: Choose the authentication method you prefer:
Password authentication: Use the master username and password.
IAM authentication: Allows you to authenticate using IAM roles.
Backup: Configure the backup settings, including the retention period for automated backups.
Maintenance: Configure the preferred maintenance window for your instance.
Monitoring: Choose if you want to enable enhanced monitoring and select the granularity of monitoring data.
Encryption: Choose whether to enable encryption at rest.
Tags (Optional): Add any tags you want to help identify your RDS instance.
Review and Launch: Review all your configuration settings. If everything looks good, click the “Create database” button.
Wait for Creation: AWS will now create your RDS instance. This might take a few minutes.
Connect to the Database: Once the RDS instance is available, you can get its endpoint information from the RDS Dashboard. Use this information to connect to the database using your preferred PostgreSQL client or programming language.
That’s it! You’ve successfully created a PostgreSQL RDS instance on AWS. Remember to monitor your instance’s performance and security settings regularly to ensure it meets your needs.
How to connect PostgreSQL RDS using EC2 instance
To connect to a PostgreSQL RDS (Amazon Relational Database Service) instance using an EC2 instance, you’ll need to follow these steps:
Launch an EC2 Instance:
If you haven’t already, launch an Amazon EC2 instance. Ensure that it’s in the same AWS region as your RDS instance, as network latency can affect connection performance.
Security Group Configuration:
Ensure that your EC2 instance and RDS instance are associated with the appropriate security groups, and that the security groups allow inbound and outbound traffic on the PostgreSQL port (usually 5432) and the necessary access between the instances. Here’s how you can configure security groups:
EC2 Security Group:
Go to the EC2 Dashboard.
Select your EC2 instance.
In the “Details” tab at the bottom, scroll down to the “Security groups” section.
Click on the associated security group to view its inbound and outbound rules.
Make sure there is an inbound rule allowing traffic on port 5432 (or the port your PostgreSQL database is using). You may need to modify the security group rules to allow traffic from the EC2 instance’s security group or specific IP addresses.
RDS Security Group:
Go to the RDS Dashboard.
Select your RDS instance.
In the “Connectivity & security” tab, locate the “Security” section.
Click on the associated security group to view its inbound and outbound rules.
Ensure there is an inbound rule allowing traffic from the EC2 instance’s security group or specific IP addresses on port 5432 (or the PostgreSQL port you’ve configured).
Install PostgreSQL Client on Ubuntu 22.04 LTS
If your EC2 instance doesn’t already have the PostgreSQL client installed, you can install it from official website.
To install the PostgreSQL client on a Linux-based system, you can use the package manager specific to your distribution. Below are instructions for some common Linux distributions:
To use the apt repository, follow these steps:
# Create the file repository configuration:
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL. # If you want a specific version, use ‘postgresql-12’ or similar instead of ‘postgresql’
sudo apt-get -y install postgresql
Connect to the RDS Database:
psql -h <RDS_endpoint> -U <username> -d <database_name>
psql --host=demo.cejluknwyvvb.ap-south-1.rds.amazonaws.com --port=5432 --username=demo --password --dbname=demo
You will be prompted to enter the password for the specified database user.
Perform Database Operations:
Once you’re connected, you can execute SQL queries and perform database operations as needed.
Remember to follow AWS security best practices, including least privilege access, to ensure the security of your RDS and EC2 instances. Additionally, consider using IAM roles and IAM database authentication for added security when connecting to your RDS instance.
In this article we have covered How to Create PostgreSQL RDS in AWS | Connect PostgreSQL RDS using EC2 Instance.