Install PostgreSQL on Debian
Install PostgreSQL on Debian
Adding PostgreSQL Repositories
There are multiple ways to add the PostgreSQL repository. Select one.
Use the Automated Configuration Script
1
2
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Add Manually
1
2
3
4
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Note for LXC:
lsb_release -csmay not resolve automatically. Specify the distro manually. For Debian 12:
1 sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Update the source list:
1
sudo apt update
Installation
Install the latest version:
1
sudo apt install postgresql
Or install a specific version:
1
sudo apt install postgresql-[version]
Enable and verify the service:
1
2
3
sudo systemctl enable postgresql.service
sudo systemctl start postgresql.service
sudo systemctl status postgresql.service
Connect using the default postgres user:
1
sudo -u postgres psql
You’ll see the prompt:
1
postgres=#
Set a strong password for the default user:
1
\password postgres
References
This post is licensed under CC BY 4.0 by the author.