> ## Documentation Index
> Fetch the complete documentation index at: https://blog.clouddley.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Deploy a PostgreSQL Database on an AWS EC2 Instance using Clouddley

> Learn how to deploy a PostgreSQL Database on an AWS EC2 Instance using Clouddley.

export const date_0 = "Updated on September 10, 2025"

Ever felt like setting up databases in the cloud takes forever? You’re not alone. The good news is, it doesn’t have to be that way. With an EC2 instance as your server and [Clouddley](https://clouddley.com) handling the heavy lifting, deploying [PostgreSQL](https://www.postgresql.org/) on AWS becomes a smooth, simple process. In this tutorial, we’ll walk through the steps so you can spin up your PostgreSQL database quickly and focus on building instead of battling setup.

## Prerequisites

* A [Clouddley account](https://clouddley.com/)
* An [AWS account](https://aws.amazon.com/)
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed and configured on your local machine
* [AWS Access Keys](https://docs.aws.amazon.com/keyspaces/latest/devguide/create.keypair.html) with permissions to create and manage EC2 instances
* [Git installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) locally
* Basic familiarity with the Linux command line

## Create an AWS EC2 Instance

Create an EC2 instance to host your database. With AWS CLI, you can quickly create one.
First, create a VPC and security group if you don't want to use the default ones.

* Create a VPC by running the command:

```bash theme={null}
aws ec2 create-vpc --cidr-block 10.0.0.0/16
```

You should see an output like below:

<Frame caption="Create a VPC on AWS">
  <img src="https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-vpc.png?fit=max&auto=format&n=wuHX6szeuQPUk6oa&q=85&s=8365b482684727bfca822e9dac2bf06f" width="1407" height="547" data-path="images/aws-create-vpc.png" />
</Frame>

<br />

* Create a security group by running the command::

```bash theme={null}
aws ec2 create-security-group \
    --group-name new-security-group \
    --description "Security group for EC2 with port open" \
    --vpc-id <vpc_id> \
    --region us-east-1
```

<Note>Replace the `vpc_id` with the one created in the previous step. </Note>
You should see an output like this:

<Frame caption="Create a Security group on AWS">
  <img src="https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-sg.png?fit=max&auto=format&n=wuHX6szeuQPUk6oa&q=85&s=4dc2f133f251e8bf2ef23726d2c7c800" width="1987" height="442" data-path="images/aws-create-sg.png" />
</Frame>

<br />

* Allow inbound traffic on port 22 (SSH) by running the command:

```bash theme={null}
aws ec2 authorize-security-group-ingress \
    --group-id <sg_id> \
    --protocol tcp \
    --port 22 \
    --cidr 0.0.0.0/0
```

This command allows SSH access to your instance.

<Note>Replace the `sg_id` with the one created in the previous step. </Note>

* To create an EC2 instance, you need to have an SSH key pair. If you don’t have one, you can generate and import one below.

<Accordion title="How to Generate and Import SSH Keys to AWS">
  If you don’t have an SSH key pair on your local machine, follow the steps below.

  * **Step 1:** To generate an SSH key, run the command:

  ```bash theme={null}
  ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  ```

  Press **Enter** to accept the default file location and choose a passphrase if you want one.

  * **Step 2:** To add your SSH key to AWS, run:

  ```bash theme={null}
  aws ec2 import-key-pair \
    --key-name my-key \
    --public-key-material file://~/.ssh/id_rsa.pub
  ```

  <Note>Replace the path to your public key if it's different.</Note>
</Accordion>

* Finally, create the EC2 instance:

```bash theme={null}
aws ec2 run-instances \
  --image-id ami-xxxxxxxx \
  --count 1 \
  --instance-type t2.micro \
  --key-name my-key \
  --security-group-ids <sg_id>
```

<Note>
  * Replace *ami-xxxxxxxx* with regions's compatible AMI
  * Replace *my-key* with your key pair name
  * Replace *sg\_id* with your security group id
</Note>

You should see the output below:

<Frame caption="Create a EC2 instance on AWS">
  <img src="https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-ec2.png?fit=max&auto=format&n=wuHX6szeuQPUk6oa&q=85&s=76480a7889b778d09ba60224dd87010f" width="1400" height="680" data-path="images/aws-create-ec2.png" />
</Frame>

<br />

Once the instance is running, note the **Public IP address** of the instance. You'll need it later.

## Deploy

Your AWS EC2 instance is now ready. Next, deploy a PostgreSQL database using Clouddley.

<b>Step 1: Access Databases</b>

* Log in to your [Clouddley account](https://app.clouddley.com/auth/signin).
* Navigate to the <b>Databases</b> on the left sidebar.
* Click on <b>Deploy Database</b>

<Frame caption="Accessing Databases">
  <img src="https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-dashboard.png?fit=max&auto=format&n=p_iVNvg5jCvbMG-4&q=85&s=98b7fbb13523d355fa5d87886511629f" width="3780" height="1705" data-path="images/Dbs-dashboard.png" />
</Frame>

<br />

<b>Step 2: Configure your virtual machine(VM)</b>

* In the **choose or add server** dropdown, select your VM if it's listed. If not, select **+ Add Virtual Machine** and enter your EC2 instance IP address as the **VM host**, along with the **VM User** and **SSH port**.
* After adding the details, verify the connection with the **Clouddley CLI (recommended)** or **SSH.**

<Accordion title="How to Install Clouddley CLI to Verify your AWS EC2 Instance">
  - Open your local machine’s command line, then connect to the remote VM you want to configure with Clouddley. Use this command to SSH into your EC2 instance:

  ```bash theme={null}
  ssh root@<your-instance-ip>
  ```

  * Install Clouddley CLI by running the command:

  ```bash theme={null}
  curl -L https://raw.githubusercontent.com/clouddley/cli/main/install.sh | sh
  ```

  * To add the SSH public key, run the command:

  ```bash theme={null}
  clouddley add key
  ```

  Using the CLI, you can deploy resources, manage configurations, and automate tasks efficiently.
</Accordion>

* Click <b>Verify</b> to check the connection.
* Once verified, click on <b>Next</b>.

<Frame caption="Configure virtual machine on Clouddley">
  <img src="https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-configure-vm.png?fit=max&auto=format&n=p_iVNvg5jCvbMG-4&q=85&s=d18addba7ce0b3a86338d06f5790100d" width="3820" height="1707" data-path="images/Dbs-configure-vm.png" />
</Frame>

<br />

<b>Step 3: Configure your database name</b>

* Enter a <b>name</b> for your PostgreSQL database.
* Click on <b>Next</b> to proceed.

<Frame caption="Defining a unique name for the PostgreSQL database">
  <img src="https://mintcdn.com/clouddley/m4JLi_8_fwaEh8mq/images/Dbs-postgres-name.png?fit=max&auto=format&n=m4JLi_8_fwaEh8mq&q=85&s=f588ec5252a7c312d2972ce97bdd102a" width="3810" height="1710" data-path="images/Dbs-postgres-name.png" />
</Frame>

<br />

<b>Step 4: Select your database type and version</b>

* Choose <b>PostgreSQL</b> as your database type.
* Select the <b>version</b> of PostgreSQL you want to deploy.
  <Tip>Ensure you select a version that is compatible with your application requirements.</Tip>
* Enable delete protection to prevent accidental deletions.
* Click on <b>Deploy DB</b>

<Frame caption="Selecting the PostgreSQL database type and specifying the version">
  <img src="https://mintcdn.com/clouddley/m4JLi_8_fwaEh8mq/images/Dbs-postgres.png?fit=max&auto=format&n=m4JLi_8_fwaEh8mq&q=85&s=420644b4f33eabc90e59b2c106b0fa9f" width="3810" height="1720" data-path="images/Dbs-postgres.png" />
</Frame>

<br />

<b>Step 5: Confirm deployment</b>

* Once your deployment is complete, you will see a confirmation message `Deployed successfully!`

<Frame caption="PostgreSQL database successfully deployed">
  <img src="https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-success.png?fit=max&auto=format&n=p_iVNvg5jCvbMG-4&q=85&s=27451216c04bccb1a40f0d4d41acb4d0" width="3815" height="1712" data-path="images/Dbs-success.png" />
</Frame>

<br />

<b>Step 6: Open database port on AWS EC2 instance</b>

* Check the port Clouddley has configured for your PostgreSQL database. Then, add a new rule to your AWS security group to allow traffic for that port. For example, if Clouddley configured port `14172`, run:

```bash theme={null}
aws ec2 authorize-security-group-ingress \
    --group-id <sg_id> \
    --protocol tcp \
    --port 14172 \
    --cidr 0.0.0.0/0
```

<Note>Replace the `sg_id` with the one created earlier. </Note>
You can now connect your PostgreSQL database to your application.

## Manage your database

The Database dashboard makes it easy to manage your PostgreSQL database on an AWS EC2 instance. You can deploy, monitor, and maintain your database.

Here’s what you can do:

* View real-time logs to check performance and troubleshoot fast.

* Reset passwords securely with built-in authentication and encryption.

* Prevent accidental deletions with built-in delete protection.

* Manage the database users and access control.

<Frame caption="Database dashboard">
  <img src="https://mintcdn.com/clouddley/m4JLi_8_fwaEh8mq/images/Dbs-postgres-overview.png?fit=max&auto=format&n=m4JLi_8_fwaEh8mq&q=85&s=849113a4ac8b7d70f7330c5468a1a550" width="3785" height="1710" data-path="images/Dbs-postgres-overview.png" />
</Frame>

Everything you need to manage your database is right where you need it.

## Conclusion

And there you have it, your PostgreSQL database is live on AWS, running on an EC2 instance with Clouddley making deployment easier. No endless trial and error. Just a clean, reliable setup you can build on. From here, you can start plugging your database into your apps, grow as your needs evolve, and feel confident knowing you’ve set it up the right way.

Got feedback or ideas to improve the experience? We’d love to [hear from you](https://clouddley.productlane.com/roadmap). We can’t wait to see what you deploy next!

<Card title="Getting started with Clouddley?" icon="user-plus" cta="Sign up today and enjoy a 30-day free trial — no credit card required" href="https://app.clouddley.com/auth/signup" horizontal>
  A backend infrastructure for your own compute. Run apps, databases, brokers, and AI workloads on your VMs, bare metal, or VPS.
</Card>

## Additional Resources

* [Clouddley Documentation](https://docs.clouddley.com/)
* [PostgreSQL Documentation](https://www.postgresql.org/docs/)
* [AWS EC2 Documentation](https://aws.amazon.com/ec2/)

<div className="flex items-center gap-2.5 mb-6 py-0 my-0">
  <img src="https://res.cloudinary.com/dkbbdg1ko/image/upload/faith-kovi-headshot_hbezxg" alt="Faith Kovi" className="w-12 h-12 rounded-full" />

  <div>
    <div className="flex items-center gap-3">
      <p className="text-sm text-gray-600 dark:text-gray-300 flex items-center gap-1 py-0 my-0">
        <span>By</span>

        <a href="https://x.com/Vera__Kaka" target="_blank" rel="noopener noreferrer" className="font-semibold">
          <span>Faith Kovi</span>
        </a>
      </p>

      <button
        onClick={async () => {
      if (navigator.share) {
        try {
          await navigator.share({
            title: document.title,
            text: "Check this out!",
            url: window.location.href,
          });
        } catch (error) {
          console.error("Error sharing:", error);
        }
      } else {
        alert("Sharing not supported on this device/browser.");
      }
    }}
        className="ml-1 hover:scale-110 transition-transform duration-200 ease-in-out"
      >
        <Icon icon="share-nodes" iconType="light" color="#D1D5DB" size="20" />
      </button>

      <button
        onClick={() => {
      navigator.clipboard.writeText(window.location.href);
      alert("Link copied!");
    }}
        className="ml-1 hover:scale-110 transition-transform duration-200 ease-in-out"
      >
        <Tooltip tip="Copied!">
          <Icon icon="clone" iconType="light" color="#D1D5DB" size="18" />
        </Tooltip>
      </button>
    </div>

    <p className="text-xs text-gray-500 py-1 my-0">
      {date_0}
    </p>
  </div>
</div>
