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:
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 -
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'
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
Accesses the PostgreSQL prompt as the default superuser and sets a secure password for it.
sudo -u postgres psql postgres
postgres=# \password postgres
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
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
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'
Installs the pgAdmin 4 desktop client for graphical management of PostgreSQL databases.
sudo apt-get install pgadmin4-desktop
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
You can visit the GitHub repository for the Practical SQL book if you are interested in it.