Setting Up PostgreSQL on an EC2 Instance: A Step-by-Step Guide

How to Install and Configure PostgreSQL on Ubuntu for AWS EC2 Instances

Anish Bilas Panta
3 min readOct 11, 2023
PostgreSQL on EC2 instance

To set up PostgreSQL on an EC2 instance running Ubuntu, you’ll need to follow several steps. Here’s a step-by-step guide to help you get PostgreSQL up and running:

Launch an EC2 Instance:

  • Log in to your AWS Management Console.
  • Go to the EC2 Dashboard and click on “Launch Instance.”
  • Choose an Ubuntu Server AMI.
  • Follow the instance creation wizard, and make sure to configure security groups to allow incoming connections to PostgreSQL (default port is 5432).

Connect to Your EC2 Instance:

  • Once your instance is running, connect to it using SSH:
ssh -i your-key.pem ubuntu@your-ec2-instance-ip

Update Your System:

  • Update the package list and upgrade installed packages to the latest versions:
sudo apt update
sudo apt upgrade

Install PostgreSQL:

  • Install PostgreSQL and its dependencies:
sudo apt install postgresql postgresql-contrib

Configure PostgreSQL:

  • By default, PostgreSQL is set up to use the “peer” authentication method, which allows you to log in to the database using the same username as your system user. To create a PostgreSQL user and database, switch to the PostgreSQL user:
sudo -u postgres psql

Create a new PostgreSQL user with a password:

CREATE USER yourusername WITH PASSWORD 'yourpassword';

Create a new PostgreSQL database and grant privileges to the user:

CREATE DATABASE yourdatabase;
GRANT ALL PRIVILEGES ON DATABASE yourdatabase TO yourusername;

Exit the PostgreSQL prompt:

\q

Configure PostgreSQL for Remote Access (Optional):

  • By default, PostgreSQL allows only local connections. If you want to access PostgreSQL from remote machines, you need to modify the PostgreSQL configuration to allow remote connections. Edit the PostgreSQL configuration file:
sudo nano /etc/postgresql/<version>/main/postgresql.conf

Change the listen_addresses value to '*' to allow connections from any IP address:

listen_addresses = '*'
  • Save the file and exit the editor.
  • Edit the pg_hba.conf file to specify which IP addresses or networks are allowed to connect. Add the following line to allow connections from any IP (use with caution, as it's not secure for production environments):
host    all             all             0.0.0.0/0            md5
  • Save the file and exit the editor.

Restart PostgreSQL:

  • Restart PostgreSQL to apply the changes:
sudo service postgresql restart

Firewall Configuration (if applicable):

  • If you’re using AWS Security Groups, make sure the security group associated with your EC2 instance allows incoming connections on port 5432 (the default PostgreSQL port).

Access PostgreSQL:

  • You can now connect to your PostgreSQL database using a PostgreSQL client like psql or a graphical tool like pgAdmin from your local machine.

That’s it! You have successfully set up PostgreSQL on your EC2 instance running Ubuntu. Make sure to follow best practices for securing your PostgreSQL installation, including setting strong passwords and limiting access to trusted IP addresses if you’ve enabled remote access.

--

--