Mục tiêu thực hành:
- Add PostgreSQL 9.6 Repository.
- Install PostgreSQL 9.6.
- Initialize PostgreSQL 9.6.
- Start/Enable PostgreSQL.
- Kiểm tra PostgreSQL Database.
- Create Databases & User.
- Hiệu chỉnh PostgreSQL Port cho phiên kết nối Remote Access.
- Cài đặt PostgreSQL Client.
- Kiểm tra danh sách Database hiện có.
Các bước triển khai:
Add PostgreSQL 9.6 Repository.
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
Install PostgreSQL 9.6.
yum install postgresql96 postgresql96-server postgresql96-contrib postgresql96-libs -y
Initialize PostgreSQL 9.6.
/usr/pgsql-9.6/bin/postgresql96-setup initdb
Start/Enable PostgreSQL.
systemctl enable postgresql-9.6.servicesystemctl start postgresql-9.6.servicesystemctl restart postgresql-9.6.servicesystemctl status postgresql-9.6.service
- Cấu hình PostgreSQL không cần xác thực đối với localhost.
nano +82 /var/lib/pgsql/9.6/data/pg_hba.conf
host all all 0.0.0.0/0 trusthost all all ::1/128 trust
- Restart PostgreSQL 9.6.
systemctl restart postgresql-9.6
systemctl status postgresql-9.6
Kiểm tra PostgreSQL Database.
[root@localhost ~]# cd /tmp[root@localhost tmp]# sudo -u postgres psqlpsql (9.6.21)Type "help" for help.postgres=# postgres=#\q[root@localhost tmp]#
Create Databases & User.
[root@localhost tmp]# cd /tmp[root@localhost tmp]# sudo -u postgres createuser fusionpbx[root@localhost tmp]# sudo -u postgres psqlpsql (9.6.21)Type "help" for help.postgres=# alter user fusionpbx with encrypted password 'password';ALTER ROLEpostgres=# postgres=#\q[root@localhost tmp]#
sudo -u postgres psql -c "CREATE DATABASE fusionpbx;"sudo -u postgres psql -c "CREATE DATABASE freeswitch;"sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD 'admin@2021';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
or
sudo -u postgres createuser fusionpbxsudo -u postgres createdb fusionpbx
[root@localhost tmp]# sudo -u postgres psqlpsql (9.6.21)Type "help" for help.postgres=# alter user fusionpbx with encrypted password 'admin@2021';ALTER ROLEpostgres=# grant all privileges on database fusionpbx to fusionpbx;GRANTpostgres=# postgres=#\q
[root@localhost tmp]# adduser fusionpbxYou have mail in /var/spool/mail/root[root@localhost tmp]# sudo passwd fusionpbxChanging password for user fusionpbx.New password: admin@2021Retype new password: admin@2021passwd: all authentication tokens updated successfully.[root@localhost tmp]# usermod -aG postgres fusionpbx[root@localhost tmp]# cat /etc/group | grep postgrespostgres:x:26:fusionpbx[root@localhost tmp]#
[root@localhost ~]# sudo passwd postgresChanging password for user postgres.New password:Retype new password:passwd: all authentication tokens updated successfully.[root@localhost ~]#
Hiệu chỉnh PostgreSQL Port cho phiên kết nối Remote Access.
- The results above (including "127.0.0.1:5432") shows that PostgreSQL is listening only for connects originating from the local computer, so we will have to edit the "postgresql.conf" configuration file. A result including "0.0.0.0:5432" indicates that PostgreSQL is already listening for remote connections.
[root@localhost tmp]# netstat -nlp | grep 5432tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 984/postmastertcp6 0 0 ::1:5432 :::* LISTEN 984/postmasterunix 2 [ ACC ] STREAM LISTENING 19190 984/postmaster /tmp/.s.PGSQL.5432unix 2 [ ACC ] STREAM LISTENING 19187 984/postmaster /var/run/postgresql/.s.PGSQL.5432[root@localhost tmp]#
- Edit postgresql.conf
[root@localhost data]# nano /var/lib/pgsql/9.6/data/postgresql.conf
- Restart PostgreSQL.
systemctl restart postgresql-9.6.service
[root@localhost data]# netstat -nlp | grep 5432tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 21986/postmasterunix 2 [ ACC ] STREAM LISTENING 52886 21986/postmaster /tmp/.s.PGSQL.5432unix 2 [ ACC ] STREAM LISTENING 52884 21986/postmaster /var/run/postgresql/.s.PGSQL.5432[root@localhost data]#
Cài đặt PostgreSQL Client.
- Kết nối tới PostgreSQL Database.
[root@localhost tmp]# su - postgresLast login: Wed May 5 17:12:18 +07 2021 on pts/2-bash-4.2$ psqlpsql (9.6.21)Type "help" for help.postgres=# \q-bash-4.2$ exitlogout[root@localhost tmp]#
- Kiểm tra kết nối tới PostgreSQL Database bằng User được khởi tạo trước đó.
[root@localhost tmp]# su - postgresLast login: Wed May 5 17:12:18 +07 2021 on pts/2-bash-4.2$ psql postgres -U postgres -W admin@2021psql: warning: extra command-line argument "admin@2021" ignoredPassword for user postgres: admin@2021psql (9.6.21)Type "help" for help.postgres=# \q-bash-4.2$ exitlogout[root@localhost tmp]#
[root@localhost tmp]# su - postgresLast login: Wed May 5 17:12:18 +07 2021 on pts/2-bash-4.2$ psql fusionpbx -U fusionpbx -W admin@2021psql: warning: extra command-line argument "admin@2021" ignoredPassword for user postgres: admin@2021psql (9.6.21)Type "help" for help.postgres=# \q-bash-4.2$ exitlogout[root@localhost tmp]#
[root@localhost ~]# sudo -u fusionpbx psqlcould not change directory to "/root": Permission deniedpsql (9.6.21)Type "help" for help.fusionpbx=> psql fusionpbxfusionpbx-> psql fusionpbx -U fusionpbxfusionpbx->
Kiểm tra danh sách Database hiện có.
- Để biết bạn đang dùng database nào, bạn gõ lệnh \c. Nếu muốn biết thêm thông tin về socket, port, bạn dùng lệnh \conninfo.
[root@localhost ~]# sudo -u postgres psqlcould not change directory to "/root": Permission deniedpsql (9.6.21)Type "help" for help.postgres=# \listList of databasesName | Owner | Encoding | Collate | Ctype | Access privileges------------+----------+----------+-------------+-------------+------------------------freeswitch | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +| | | | | postgres=CTc/postgres +| | | | | fusionpbx=CTc/postgresfusionpbx | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +| | | | | postgres=CTc/postgres +| | | | | fusionpbx=CTc/postgrespostgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| | | | | postgres=CTc/postgrestemplate1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| | | | | postgres=CTc/postgres(5 rows)postgres=#
postgres=# \cYou are now connected to database "postgres" as user "postgres".postgres=#
postgres=# \conninfoYou are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".postgres=#
Mọi thắc mắc, các bạn vui lòng liên hệ với mình thông qua kênh Zalo (Jade Bùi) 076.877.2021.
0 comments