1. 程式人生 > >CentOS7安裝配置PostgreSQL9.6

CentOS7安裝配置PostgreSQL9.6

list 用戶密碼 創建數據庫 同名 current har -a -- tcp

參考文檔:

  1. 安裝初始化:https://www.postgresql.org/download/linux/redhat/
  2. 簡單使用:https://mozillazg.github.io/2014/06/hello-postgresql.html

本文涉及postgresql的yum安裝,訪問配置及簡單使用。

一.驗證環境

1. 操作系統

CentOS-7-x86_64-Everything-1511

2. PostgresSQL版本

PostgreSQL 9.6.3:https://www.postgresql.org/download/linux/redhat/

二.安裝

1. 安裝rpm

[[email protected]_master ~]#
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

2. 安裝客戶端

[[email protected]_master ~]# yum install -y postgresql96

3. 安裝服務器端

#yum安裝postgresql,默認會建一個名為”postgres”的系統賬號,用於執行PostgreSQL;
#同時數據庫中也會生成一個名為”postgres”的數據庫用戶,且密碼已自動生成,需要進入數據庫後修改;
#PostgreSQL在數據庫用戶同名的系統賬號下登錄免密。 [[email protected]_master ~]# yum install -y postgresql96-server

4. 初始化

[[email protected]_master bin]# /usr/pgsql-9.6/bin/postgresql96-setup initdb

5. 設置開機啟動

[[email protected]_master ~]# systemctl enable postgresql-9.6

6. 啟動

[[email protected]_master ~]#
systemctl start postgresql-9.6

三.配置使用

1. 修改用戶密碼

#yum安裝postgresql,默認會建一個名為”postgres”的系統賬號,用於執行PostgreSQL;
[[email protected]_master ~]# su - postgres

#切換用戶後,提示符變更為“-bash-4.2$”;
#同時數據庫中也會生成一個名為”postgres”的數據庫用戶,且密碼已自動生成;
#PostgreSQL在數據庫用戶同名的系統賬號下登錄免密;
-bash-4.2$ psql -U postgres

#進入數據庫後修改密碼;
postgres=# alter user postgres with password ‘[email protected]‘

技術分享

2. 允許遠程訪問

#配置文件中,默認只能本機訪問postgresql;
#修改listen_addresses = ‘localhost‘為listen_addresses = ‘*‘,允許所有遠程訪問;
#修改配置文件需要重啟服務。
[[email protected]_master ~]# sed -i "s|#listen_addresses = ‘localhost‘|listen_addresses = ‘*‘|g" /var/lib/pgsql/9.6/data/postgresql.conf

3. 主機認證

#在第82行之後,”IPv4 local connections”下新增允許的客戶端;
#“host” 代表主機類型,第一個“all”代表db ,第二個“all”代表user ,“172.29.3.67/32” 代表client ip,“trust”代表認證方式;
#認證方式除“trust”外,還有“peer”, “ident”, “md5”, “password”等,具體可參考pg-hba文件: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
#修改pg.hba文件需要重啟服務。
[[email protected]_master ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf
host    all             all             172.29.3.67/32          trust

技術分享

4. 設置環境變量

[[email protected]_master ~]# vim /etc/profile
export PATH=$PATH:/usr/pgsql-9.6/bin

[[email protected]_master ~]# source /etc/profile

5. 重啟服務

[[email protected]_master ~]# systemctl restart postgresql-9.6

6. iptables

#postgresql默認開啟tcp5432端口
[[email protected]_master ~]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

[[email protected]_master ~]# service iptables restart

四.使用驗證

1. 查看端口

[[email protected]_master ~]# netstat -tunlp

技術分享

2. 簡單使用

1)創建用戶

postgres=# create user postuser1 with password ‘[email protected]‘;

2)創建數據庫

#同時指定數據庫的所有者
postgres=# create database postdb1 owner postuser1;

3)數據庫賦權

#未賦權則賬戶只能登錄控制臺
postgres=# grant all privileges on database postdb1 to postuser1;

技術分享

4)登錄新建數據庫

#在操作系統層使用新建的賬號登錄新建的數據庫,登錄後提示符為“postdb1=>”;
#如果在postgres賬戶下直接使用“postgres=# \c postdb1;”登錄,則登錄用戶依然是postgres,
-bash-4.2$ psql -U postuser1 -d postdb1 -h 127.0.0.1 -p 5432

技術分享

5)創建表

postdb1=> create table tb1(
          id int primary key,
          name VARCHAR(20), 
          salary real
          );

6)插入數據

postdb1=> insert into tb1(
          id, name, salary)
          values(
          101, Mike, 5000.00
          );

7)查詢

postdb1=>select * from tb1;

技術分享

3. pgadmin連接postgresql

pgadmin下載地址:https://www.pgadmin.org/download/

截至2017-05-19的版本是:pgAdmin 4 v1.5

1)添加服務器

打開pgadmin—>添加新的服務器—>(通常標簽)名稱自定義—>(connection標簽)主機名稱與postgresql用戶密碼按需填寫,其余可采用默認配置—>保存。技術分享

技術分享

2)圖形化查看

技術分享

CentOS7安裝配置PostgreSQL9.6