添加RPM
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
安裝PostgreSQL 9.6
postgresql96-server 數據庫核心服務端
postgresql96-contrib 附加第三方擴展
postgresql96-devel C語言開發Header頭文件和庫(可以不裝)
yum install postgresql96-server postgresql96-contrib postgresql96-devel
驗證是否安裝成功
rpm -aq| grep postgres
更改數據庫數據存儲路徑(原默認路徑爲:/var/lib/pgsql/9.6/data )
cd /mnt
sudo mkdir vdb1
sudo chown -R postgres:postgres vdb1
sudo chmod 700 vdb1
vi /usr/lib/systemd/system/postgresql-9.6.service
初始化數據庫
/usr/pgsql-9.6/bin/postgresql96-setup initdb
開啓服務
service postgresql-9.6start 或者 systemctl start postgresql-9.6.service
開機啓動
sudo chkconfig postgresql-9.6 on 或者 systemctl enable postgresql-9.6.service
修改密碼
su postgres
psql
ALTER USER postgres WITH PASSWORD '密碼'; --必須以分號結束,成功執行後會出現ALTER ROLE
\q
su root
開啓遠程訪問
vi /var/lib/pgsql/9.6/data/postgresql.conf 或者 vi /mnt/vdb1/postgresql.conf
修改#listen_addresses = 'localhost' 爲 listen_addresses='*'
當然,此處‘*’也可以改爲任何你想開放的服務器IP
信任遠程連接
vi /var/lib/pgsql/9.6/data/pg_hba.conf 或者 vi /mnt/vdb1/pg_hba.conf
修改如下內容,信任指定服務器連接
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 10.211.55.6/32(需要連接的服務器IP,或者直接改爲0.0.0.0/0,表示任意服務器連接) md5
重啓服務
service postgresql-9.6 restart 或者 systemctl restart postgresql-9.6.service
配置防火牆
CentOS 防火牆中內置了PostgreSQL服務,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我們只需以服務方式將PostgreSQL服務開放即可。
systemctl enable firewalld 開機啓用防火牆
systemctl start firewalld 開啓防火牆
firewall-cmd --add-service=postgresql --permanent 開放postgresql服務
firewall-cmd --zone=public --add-port=5432/tcp --permanent 或者可以直接添加端口
firewall-cmd --reload 重載防火牆
firewall-cmd --list-ports 查看佔用端口
簡單使用
psql -U postgres postgres 連接數據庫
說明:-h表示主機(Host),-p表示端口(Port),-U表示用戶(User)
顯示所有數據庫: \l
卸載PostgreSQL
yum erase postgresql96
結束
備註:
[root@ip-10-0-0-228 ec2-user]# service postgresql initdbData directory is not empty! [FAILED]
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228
ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 88K
-rw——- 1 postgres postgres 4 May 5 09:32 PG_VERSION
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_twophase
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_tblspc
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_stat_tmp
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_snapshots
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_serial
drwx—— 4 postgres postgres 4.0K May 5 09:32 pg_multixact
-rw——- 1 postgres postgres 20K May 5 09:32 postgresql.conf
-rw——- 1 postgres postgres 1.6K May 5 09:32 pg_ident.conf
-rw——- 1 postgres postgres 4.2K May 5 09:32 pg_hba.conf
drwx—— 3 postgres postgres 4.0K May 5 09:32 pg_xlog
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_subtrans
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_clog
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_notify
drwx—— 2 postgres postgres 4.0K May 5 09:32 global
drwx—— 5 postgres postgres 4.0K May 5 09:32 base
drwx—— 2 postgres postgres 4.0K May 5 09:32 pg_log
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
rm -rf /var/lib/pgsql9/data/*
[root@ip-10-0-0-228 ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 0
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# service postgresql initdb
Initializing database: [ OK ]
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]#
[root@ip-10-0-0-228 ec2-user]# ll -lhtr /var/lib/pgsql9/data/
total 88K
-rw——- 1 postgres postgres 4 May 7 10:42 PG_VERSION
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_twophase
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_tblspc
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_stat_tmp
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_snapshots
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_serial
drwx—— 4 postgres postgres 4.0K May 7 10:42 pg_multixact
-rw——- 1 postgres postgres 20K May 7 10:42 postgresql.conf
-rw——- 1 postgres postgres 1.6K May 7 10:42 pg_ident.conf
-rw——- 1 postgres postgres 4.2K May 7 10:42 pg_hba.conf
drwx—— 3 postgres postgres 4.0K May 7 10:42 pg_xlog
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_subtrans
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_clog
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_notify
drwx—— 2 postgres postgres 4.0K May 7 10:42 global
drwx—— 5 postgres postgres 4.0K May 7 10:42 base
drwx—— 2 postgres postgres 4.0K May 7 10:42 pg_log
[root@ip-10-0-0-228 ec2-user]#
轉載於Qbit編程學習筆記