Created
December 8, 2025 01:17
-
-
Save rafirh/78abace1c6ffbc1d3be10e30d1b54eab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| echo "=== AUTO INSTALL POSTGRESQL + POSTGIS ===" | |
| # -------- INPUT USER -------- | |
| read -p "Masukkan nama database: " DB_NAME | |
| read -p "Masukkan username database: " DB_USER | |
| read -sp "Masukkan password database: " DB_PASS | |
| echo "" | |
| read -p "Masukkan port PostgreSQL (default 5432): " DB_PORT | |
| DB_PORT=${DB_PORT:-5432} | |
| echo "" | |
| echo ">>> Database: $DB_NAME" | |
| echo ">>> Username: $DB_USER" | |
| echo ">>> Port: $DB_PORT" | |
| echo "" | |
| sleep 2 | |
| # -------- INSTALL POSTGRESQL -------- | |
| echo "=== INSTALL POSTGRESQL ===" | |
| # Add PostgreSQL repository | |
| sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | |
| wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
| # Update dan install PostgreSQL 14 | |
| sudo apt update | |
| sudo apt install -y postgresql-14 postgresql-contrib-14 | |
| # -------- INSTALL POSTGIS -------- | |
| echo "=== INSTALL POSTGIS ===" | |
| sudo apt install -y postgresql-14-postgis-3 postgis | |
| # -------- START SERVICE -------- | |
| echo "=== START POSTGRESQL SERVICE ===" | |
| sudo systemctl start postgresql | |
| sudo systemctl enable postgresql | |
| # -------- CONFIGURE PORT -------- | |
| if [ "$DB_PORT" != "5432" ]; then | |
| echo "=== CONFIGURE PORT ($DB_PORT) ===" | |
| sudo sed -i "s/^#port = 5432/port = $DB_PORT/" /etc/postgresql/14/main/postgresql.conf | |
| sudo sed -i "s/^port = 5432/port = $DB_PORT/" /etc/postgresql/14/main/postgresql.conf | |
| sudo systemctl restart postgresql | |
| fi | |
| # -------- CREATE USER & DATABASE -------- | |
| echo "=== CREATE USER & DATABASE ===" | |
| # Create user | |
| sudo -u postgres psql -p $DB_PORT -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" | |
| # Create database | |
| sudo -u postgres psql -p $DB_PORT -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;" | |
| # Enable PostGIS extension | |
| sudo -u postgres psql -p $DB_PORT -d $DB_NAME -c "CREATE EXTENSION IF NOT EXISTS postgis;" | |
| sudo -u postgres psql -p $DB_PORT -d $DB_NAME -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;" | |
| # Grant privileges | |
| sudo -u postgres psql -p $DB_PORT -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" | |
| # -------- CONFIGURE REMOTE ACCESS -------- | |
| echo "=== CONFIGURE REMOTE ACCESS ===" | |
| # Allow password authentication | |
| sudo sed -i "s/^local.*all.*all.*peer/local all all md5/" /etc/postgresql/14/main/pg_hba.conf | |
| sudo sed -i "s/^host.*all.*all.*127.0.0.1\/32.*scram-sha-256/host all all 127.0.0.1\/32 md5/" /etc/postgresql/14/main/pg_hba.conf | |
| # Allow connections from any IP (for remote access) | |
| echo "host all all 0.0.0.0/0 md5" | sudo tee -a /etc/postgresql/14/main/pg_hba.conf > /dev/null | |
| # Listen on all addresses | |
| sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/14/main/postgresql.conf | |
| sudo sed -i "s/^listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/14/main/postgresql.conf | |
| # Restart PostgreSQL | |
| sudo systemctl restart postgresql | |
| # -------- TEST CONNECTION -------- | |
| echo "=== TEST CONNECTION ===" | |
| sudo -u postgres psql -p $DB_PORT -d $DB_NAME -c "SELECT version();" | |
| sudo -u postgres psql -p $DB_PORT -d $DB_NAME -c "SELECT PostGIS_version();" | |
| echo "====================================" | |
| echo "INSTALL SELESAI!" | |
| echo "" | |
| echo "Connection String:" | |
| echo "postgresql://$DB_USER:$DB_PASS@localhost:$DB_PORT/$DB_NAME" | |
| echo "" | |
| echo "Untuk akses remote, gunakan IP server:" | |
| echo "postgresql://$DB_USER:$DB_PASS@<SERVER_IP>:$DB_PORT/$DB_NAME" | |
| echo "" | |
| echo "PostGIS extension sudah aktif!" | |
| echo "====================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment