Search Posts

Install Postgres

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

  1. Switch to the postgres user:
    sudo -i -u postgres
  2. Create a new database user (replace myuser with your desired username):
    createuser myuser --interactive
  3. Create a new database (replace mydatabase with your desired database name):
    createdb mydatabase

    Step 5: Enable PostGIS Extension on the Database

  4. Connect to the database:
    psql mydatabase
  5. Enable the PostGIS extension:
    CREATE EXTENSION postgis;
  6. Verify the installation:
    \dx

    Step 6: Exit PostgreSQL Shell

    \q

    Step 7: Exit postgres User Shell

    exit

    Additional Configuration
    You might need to configure PostgreSQL to accept remote connections or adjust authentication methods, depending on your requirements.
    To allow remote connections:

  7. Edit the PostgreSQL configuration file:
    sudo nano /etc/postgresql/14/main/postgresql.conf
  8. Find the line #listen_addresses = 'localhost' and change it to:
    listen_addresses = '*'
  9. Edit the pg_hba.conf file to add your client's IP address:
    sudo nano /etc/postgresql/14/main/pg_hba.conf
  10. Add the following line at the end of the file:
    host    all             all             0.0.0.0/0               md5
  11. Restart PostgreSQL:
    sudo systemctl restart postgresql

    set the password to examplepassword, the commands would be:

    sudo -i -u postgres
    psql
    ALTER USER exampleuser WITH PASSWORD 'examplepassword';