1、配置YUM庫
vi /etc/yum.repos.d/CentOS-Base.repo
- 1
在
[base]
和
[updates]
區段添加:
exclude=postgresql*
- 1
配置完yum庫之後解除安裝之前安裝的Postgresql:
yum erase postgresql*
- 1
為了避免有遺留,可以多删除幾遍,直到有如下提示:
已加載插件:fastestmirror, langpacks
參數 postgresql 沒有比對
不删除任何軟體包
- 1
- 2
- 3
删除所有資料:
rm -rf /var/lib/pgsql
- 1
2、安裝PGDG RPM檔案
打開網頁(下載下傳yun源), 找到正确的RPM。
比如,為CentOS7 64位的系統,安裝PostgreSQL 9.5:
打開(http://yum.postgresql.org/repopackages.php#pg95)後,找到CentOS 7 - x86_64
然後,執行如下指令:
yum localinstall https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
- 1
安裝contrib第三方包和分布式包,可以選擇不安裝:
yum -y install postgresql95-server postgresql95-contrib
- 1
3、初始化資料庫:
/usr/pgsql-9.5/bin/postgresql95-setup initdb
- 1
4、Postgresql服務控制:
service postgresql-9.5 start/stop/restart/reload
- 1
5、開機自啟動
sudo chkconfig postgresql-9.5 on
- 1
6、解除安裝
yum erase postgresql95
- 1
yum安裝官方資料說明:yum安裝說明
7、 修改密碼:
修改linux系統使用者postgres的密碼,PostgreSQL 資料庫預設會建立一個linux 系統使用者postgres,通過passwd 指令可設定系統使用者的密碼。
直接在控制台輸入以下指令:
su postgres
psql
ALTER USER postgres WITH PASSWORD 'postgresql';
\q
su root
- 1
- 2
- 3
- 4
- 5
這樣就把
postgres
的密碼改為
postgresql
了。
8、修改PostgresSQL資料庫配置實作遠端通路
①、修改
postgresql.conf
檔案:
vi /var/lib/pgsql/9.5/data/postgresql.conf
- 1
将
listen_addresses
前的
#
去掉,并将
listen_addresses = 'localhost'
改成
listen_addresses = '*'
;
②、修改用戶端認證配置檔案pg_hba.conf,将需要遠端通路資料庫的IP位址或位址段加入該檔案。
vi /var/lib/pgsql/9.5/data/pg_hba.conf
- 1
将IPv4區下的
127.0.0.1/32
修改為
0.0.0.0/0
; 将ident修改為md5
③、重新開機服務使設定生效
service postgresql-9.5 restart
- 1
9、設定防火牆,開放5432端口:
#為public域開放tcp協定的5432端口
firewall-cmd --zone=public --add-port=5432/tcp --permanent
#為public域添加http服務
firewall-cmd --zone=public --add-service=postgresql --permanent
#重新開機firewall服務
firewall-cmd --reload
- 1
- 2
- 3
- 4
- 5
- 6
10、簡單使用:
連接配接資料庫:
psql -h 127.0.0.1 -p 5432 -U postgres testdb
- 1
顯示所有資料庫:
\l
- 1
建立資料庫:
createdb -h 127.0.0.1 -p 5432 -U postgres testdb
- 1
删除資料庫:
dropdb -h 127.0.0.1 -p 5432 -U postgres testdb
- 1
說明:-h表示主機(Host),-p表示端口(Port),-U表示使用者(User)
建立資料表:
create table tbl(a int);
- 1
删除資料表:
drop table tbl;
- 1
插入資料:
insert into tbl(a) values(1);
- 1
檢視資料:
select * from tbl;
- 1
備份資料庫:
pg_dump -U postgres testdb > d:/testdb.dmp
- 1
恢複資料庫:
psql -U postgres testdb < d:/testdb.dmp
- 1
說明:這種方法,實際上是SQL的轉儲,可加參數(-t)指定多個表。
退出:
\q
- 1
這樣就完成了PostgreSql的安裝,使用方法除了可以參考以上的說明外還可以參考Wiki。
頂
1
轉載于:https://my.oschina.net/u/3197790/blog/1538390