After referring many blog posts and working through many error messages, I found the instructions that work. We will:
Launch an EC2 instance
Download the PEM file
Connect to the EC2 instance
Manually install PostgreSQL
First step is to launch an EC2 instance using the AWS console. Then, connect to the EC2 instance using SSH. Finally, install the PostgreSQL 16 server manually. Once we have a working database, we will copy the commands to the shell provisioner block in Packer template to automate the installation.
Caution
If you install Postgresql that comes with the builtin apt package, you will get old version of Postgresql.
Download the PEM file from AWS console. Save it on your local machine.
Change the server name ec2-12-123-45-67.us-west-2.compute.amazonaws.com for your EC2 instance. This can be found in the networking connection section of the EC2 instance.
Check Ubuntu version:
The following are already installed by default on the EC2 instance:
Software
Version
Git
2.34.1
Wget
1.21.2
Curl
7.81.0
This pops up window for restarting services. To turn off the popup:
The current Postgresql version is 16. To install this version:
This results in error:
The reason is that the packages are not available in the default repository for this version of Ubuntu.
Run the following commands:
You can see the version of the installed database:
You can see the database users and their permissions:
After installation, PostgreSQL will be running by default. If you want to start:
Setup the database as a service that runs when the server boots up:
The postgresql-client-16 package provides the necessary tools for managing and interacting with PostgreSQL databases, whether for development, administration, or automation tasks, without needing to install the full PostgreSQL server package on the client machine.
The authentication method a PostgreSQL user must use to access the psql prompt is determined by PostgreSQL’s own configuration in the pg_hba.conf file, and it is independent of whether a Linux system user is part of the sudo group. Thus, a deploy PostgreSQL user will need to use a password if the pg_hba.conf file is configured to require password authentication (e.g., md5 or scram-sha-256) for their connections.
The contents of /etc/postgresql/16/main/pg_hba.conf file:
The line number 6 is related to local connections:
Change peer to md5:
This change requires users to authenticate with a password when connecting locally.
Caution
If this configuration change is not made, you will get the error:
psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: FATAL: Peer authentication failed for user “postgres”
When you try to connect:
The changes to pg_hba.conf file should be made with a user that has sudo privileges. The ubunutu user has sudo privileges by default. We provided sudo privileges to deploy user. So we can use any of these two users to make the changes.