Automated repository configuration:
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Step 1: Update the Package List
sudo apt update
Step 2: Install PostgreSQL
sudo apt install -y postgresql postgresql-contrib
Step 3: Install PostGIS
sudo apt install -y postgis postgresql-16-postgis-3
replace the 16 with version i have installed
to check the version
psql --version
Step 4: Create a PostgreSQL User and Database
- Switch to the postgres user:
sudo -i -u postgres - Create a new database user (replace myuser with your desired username):
createuser myuser --interactive - Create a new database (replace mydatabase with your desired database name):
createdb mydatabaseStep 5: Enable PostGIS Extension on the Database
- Connect to the database:
psql mydatabase - Enable the PostGIS extension:
CREATE EXTENSION postgis; - Verify the installation:
\dxStep 6: Exit PostgreSQL Shell
\qStep 7: Exit postgres User Shell
exitAdditional Configuration
You might need to configure PostgreSQL to accept remote connections or adjust authentication methods, depending on your requirements.
To allow remote connections: - Edit the PostgreSQL configuration file:
sudo nano /etc/postgresql/14/main/postgresql.conf - Find the line #listen_addresses = 'localhost' and change it to:
listen_addresses = '*' - Edit the pg_hba.conf file to add your client's IP address:
sudo nano /etc/postgresql/14/main/pg_hba.conf - Add the following line at the end of the file:
host all all 0.0.0.0/0 md5 - Restart PostgreSQL:
sudo systemctl restart postgresqlset the password to examplepassword, the commands would be:
sudo -i -u postgres psql ALTER USER exampleuser WITH PASSWORD 'examplepassword';