Skip to content

Instantly share code, notes, and snippets.

@jerryperezperez
Last active June 21, 2025 20:27
Show Gist options
  • Save jerryperezperez/cddecc920ccdaca064c51d58c5c9ac5f to your computer and use it in GitHub Desktop.
Save jerryperezperez/cddecc920ccdaca064c51d58c5c9ac5f to your computer and use it in GitHub Desktop.
Installation of PostgreSQL and PGAdmin for Debian-based OS

Installation of PostgreSQL and pgAdmin for Debian-based OS

This documentation is taken from the Practical SQL book by No Starch Press - Anthony DebBarros, which is in fact taken from PostgreSQL and Ubuntu's official documentation you can find at:

Step 1: Import the key for the PostgreSQL APT repository

This step installs required tools and imports the official PostgreSQL signing key to verify package authenticity.

sudo apt-get install curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Step 2: Create the file /etc/apt/sources.list.d/pgdg.list

Adds the PostgreSQL APT repository for your system’s Debian-based version to your package sources.

sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Step 3: Update the package list and install PostgreSQL

Refreshes your package cache and installs PostgreSQL 13 from the newly added repository. You can replace the postgresql version to download.

sudo apt-get update
sudo apt-get install postgresql-13

Step 4: Log in to the PostgreSQL server as postgres user and set a root password

Accesses the PostgreSQL prompt as the default superuser and sets a secure password for it.

sudo -u postgres psql postgres
postgres=# \password postgres

Step 5: Create a SUPERUSER

Creates a new PostgreSQL user with superuser privileges. Replace ${user} with your desired username and then exit the psql console. The author recommends to create a superuser that aligns with the OS's user.

postgres=# CREATE USER ${user} SUPERUSER;
postgres=# \q

Step 6: Impor the key for the pgAdmin repository

Imports the signing key for the pgAdmin repository to authenticate its packages.

curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add

Step 7: Create the file /etc/apt/sources.list.d/pgdg.list

Adds the pgAdmin APT repository matching your Ubuntu version to your package sources and updates the package list.

sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

Step 8: Install pgAdmin 4

Installs the pgAdmin 4 desktop client for graphical management of PostgreSQL databases.

sudo apt-get install pgadmin4-desktop

Step 9: Install PostGIS and PL/Python extensions (if needed)

Optionally installs the PostGIS extension for spatial data support and PL/Python to enable Python scripting in PostgreSQL.

sudo apt install postgresql-13-postgis-3
sudo apt install postgresql-plpython3-13

Further documentation

You can visit the GitHub repository for the Practical SQL book if you are interested in it.

https://github.com/anthonydb/practical-sql-2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment