PostgreSQL atau Postgres adalah objek-relational database yang opensource dengan banyak fitur lanjutan yang memungkinkan kamu untuk membuat aplikasi web yang rumit.
Dalam tutorial ini, ayies akan menunjukkan cara menginstal PostgreSQL terbaru di mesin CentOS 7. Ayies akan menunjukkan cara menginstal versi terbaru PostgreSQL dari repositori resmi PostgreSQL.
Jika aplikasi Anda tidak memerlukan versi terbaru, sebaiknya gunakan versi database yang lama saja yang biasanya tersedia secara default di repository Centos.
Enable Repository Postgres versi terbaru (10):
sudo yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
Jalankan proses install sebagai berikut:
sudo yum install postgresql10-server postgresql10-contrib
sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
Lakukan proses konfigurasi yaitu:
sudo systemctl start postgresql-10
sudo systemctl enable postgresql-10
sudo -u postgres /usr/pgsql-10/bin/psql -c "SELECT version();"
Output:
PostgreSQL 10.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28), 64-bit
(1 row)
Postgresql Role dan Authentication
sudo su - postgres
psql
create role john;
create database johndb;
grant all privileges on database johndb to john;
Enable Remote Postgresql
Edit file dan ubah menjadi * pada listen address
sudo vim /var/lib/pgsql/10/data/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
sudo systemctl restart postgresql-10
Sebagai tambahan ubah juga menjadi 0.0.0.0/0 yang berarti bisa diakses dari semua IP
sudo vimĀ /var/lib/pgsql/10/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# The user jane will be able access all databases from all locations using a md5 password
host all jane 0.0.0.0/0 md5
# The user jane will be able access only the janedb from all locations using a md5 password
host janedb jane 0.0.0.0/0 md5
# The user jane will be able access all databases from a trusted location (192.168.1.134) without a password
host all jane 192.168.1.134 trust
Restart kembali postgres.