The following scripts install PostgreSQL on Ubuntu 22.04
Installation Steps
Perform Updates
First install any updates for Ubuntu
sudo apt update
sudo apt upgrade -y
Add Repository and Install
Ubuntu does not include PostgreSQL so we must add the repository manually.
# Add the repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Add the signing keys
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package list to include the new repository
sudo apt update
# Perform the install
sudo apt install postgresql-15 -y
Start and Enable PostgreSQL
Once installed, we will start and enable PostgreSQL. This ensures that the service starts up after a reboot.
# Start and enable the PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Ensure the service is running
sudo systemctl status postgresql
# Connect to PostgreSQL locally
sudo -i -u postgres
psql