Install PostgreSQL on AWS EC2 Linux: A Step-by-Step Guide

Updated

install postgresql on aws ec2 linux

On Amazon Linux 2023, PostgreSQL is in AL2023’s own repositories, so this is three commands. The package name must carry the major version — there is no bare postgresql-server package on AL2023.

sudo dnf install -y postgresql17-server
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql

A note on what is verified here. I have no EC2 instance and no Linux host, so no dnf or systemctl output appears on this page and none has been invented. What I did run, from a shell on 2026-08-10, is HTTP against the two repositories involved, and I downloaded and parsed Amazon Linux 2023’s published package metadata. Those results are marked measured. The rest is quoted from AWS and postgresql.org.

Why the amazon-linux-extras sequence no longer works

The sequence sudo amazon-linux-extras install postgresql10, then sudo service postgresql initdb and sudo chkconfig postgresql, has expired in every part:

  • amazon-linux-extras does not exist on Amazon Linux 2023. From the AL2023 FAQ: “Q: Does AL2023 have Amazon-Linux-Extras like AL2? A: No, AL2023 does not have extras.” Versioned packages replaced it, which is why the package is postgresql17-server and not postgresql-server.
  • Amazon Linux 2 is out of support. AWS: “Amazon Linux 2 reached its end of support on 2026-06-30.” An article that tells you to launch AL2 in 2026 is telling you to launch an unpatched OS.
  • PostgreSQL 10 has been unsupported since 10 November 2022, per the postgresql.org versioning table (measured: I fetched that table on 2026-08-10). The oldest supported major is 14, and its final release is 12 November 2026, so do not start a new install on it.

service and chkconfig belong to the pre-systemd era; AL2023 uses systemd, which is why the commands above are systemctl.

What Amazon Linux 2023 actually ships (measured)

I pulled AL2023’s current primary.xml.gz from cdn.amazonlinux.com/al2023/core/ and grepped it for server packages. As of 2026-08-10 the core repository carries:

postgresql15-server  ... 15.18
postgresql16-server  ... 16.14
postgresql17-server  ... 17.10
postgresql18-server  ... 18.3, 18.4
bare "postgresql-server" package present: False

Those are the same minor versions postgresql.org lists as current, so AL2023’s own packages are not behind. Pick one major; the packages conflict with each other by design, so you cannot install 16 and 17 side by side from this repo.

If you want the PGDG repository instead

The PostgreSQL project’s own RPM repository is aimed at RHEL/Rocky/AlmaLinux. Checked on 2026-08-10 (measured, HTTP status codes from curl):

200  https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
200  https://download.postgresql.org/pub/repos/yum/reporpms/EL-10-x86_64/pgdg-redhat-repo-latest.noarch.rpm
major version trees under /pub/repos/yum/ : 14 15 16 17 18
410  https://download.postgresql.org/pub/repos/yum/13/     (Gone -- PostgreSQL 13 is EOL)

On RHEL 9 or a rebuild, the current sequence from the project’s own download page is:

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf install -y postgresql18-server
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
sudo systemctl enable postgresql-18
sudo systemctl start postgresql-18

Note the unit is postgresql-18, not postgresql, and the setup binary lives under /usr/pgsql-18/bin/. Distro packages and PGDG packages use different names for both — mixing the two sets of instructions is the most common way this install goes wrong.

Do not assume that repository works on Amazon Linux 2023. yum.postgresql.org lists its supported platforms as RHEL/Rocky/AlmaLinux 8, 9 and 10 and Fedora; Amazon Linux is not mentioned anywhere on that page, in either direction. Two concrete obstacles show up in the metadata (measured from the published RPM headers, not from an attempted install): pgdg-redhat-repo requires the file /etc/redhat-release, which no AL2023 package provides, and postgresql18-server from PGDG requires libldap.so.2, while AL2023’s core repository ships only libldap-2.4.so.2. Treat “install the PGDG EL-9 repo on AL2023” as unverified at best. Use AL2023’s own packages.

Authentication: scram-sha-256, not md5

Do not edit pg_hba.conf to change settings from ident to md5. The PostgreSQL documentation carries an explicit warning: “Support for MD5-encrypted passwords is deprecated and will be removed in a future release of PostgreSQL,” and describes scram-sha-256 as “the most secure of the currently provided methods.” password_encryption has defaulted to scram-sha-256 since PostgreSQL 14, so a fresh install already stores SCRAM verifiers — writing md5 in pg_hba.conf just downgrades the handshake.

# /var/lib/pgsql/data/pg_hba.conf
local   all   all                     peer
host    all   all   127.0.0.1/32      scram-sha-256
host    all   all   10.0.0.0/16       scram-sha-256   # your VPC CIDR, never 0.0.0.0/0

Then sudo systemctl reload postgresql — pg_hba.conf changes need a reload, not a restart.

What is specific to EC2

PostgreSQL listens on localhost only until you change listen_addresses, and that is the safest default on a public cloud host. If you open it up, open it narrowly: set listen_addresses to the instance’s private address, put 5432 in a security group that references another security group rather than a CIDR, and keep the instance in a private subnet. A security group rule allowing 0.0.0.0/0 on 5432 puts your database in the internet’s port-scan results within minutes. Snapshot the EBS volume holding /var/lib/pgsql on a schedule, and remember that an EC2 instance store is lost on stop — if you put a data directory there, you will lose it.

If this is a production database rather than a lab, price RDS or Aurora before you build it yourself: you are otherwise signing up to run backups, minor-version upgrades and failover by hand for the life of the system.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.