YuChun's Blog
Published on

How to install and setup PostGIS on Ubuntu 22.04?

Authors
  • avatar
    Name
    YuChun Tsao
    Twitter

Install PostgreSQL

Import the repository key from https://www.postgresql.org/media/keys/ACCC4CF8.asc

sudo apt install curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null

Create source list with your distribution

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

Update and install package

sudo apt update
sudo apt install -y postgresql

You can also specify the version you want to install.

sudo apt install -y postgresql-15

Uninstall command: sudo apt --purge autoremove postgresql

Install PostGIS

sudo apt install -y postgis

Uninstall command: sudo apt --purge autoremove postgis

Setup Database

Create user

sudo -u postgres createuser username

Using password authentication to connect database, you can use -P or --pwprompt to set user password.

sudo -u postgres createuser username -P

More information about PostgreSQL Authentication Methods

Create database

Set the owner of this database to username

sudo -u postgres createdb --encoding=UTF8 --owner=username dbname

Create PostGIS extension

sudo -u postgres psql dbname --command='CREATE EXTENSION postgis;'

Connect to database with username

psql -h localhost -U username -d dbname -W

You can also link to this database through QGIS

qgis-postgis-connection-information

References