1、驗證環境
- 作業系統 CentOS-7-x86_64-Everything-1511
- postgresql版本 PostgreSQL 9.6.3:https://www.postgresql.org/download/linux/redhat/
2、安裝
- 安裝rpm
[root@psql_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
- 安裝客戶端
[root@psql_master ~]# yum install -y postgresql96
- 安裝服務器端
#yum安裝postgresql,默認會建一個名為”postgres”的系統賬號,用于執行PostgreSQL; #同時資料庫中也會生成一個名為”postgres”的資料庫用戶,且密碼已自動生成,需要進入資料庫后修改; #PostgreSQL在資料庫用戶同名的系統賬號下登錄免密, [root@psql_master ~]# yum install -y postgresql96-server - 初始化
[root@psql_master ~]# /usr/pgsql-9.6/bin/postgresql96-setup initdb
- 設定開機啟動
[root@psql_master ~]# systemctl enable postgresql-9.6
- 啟動
[root@psql_master ~]# systemctl start postgresql-9.6
3、配置使用
- 修改用戶密碼
#yum安裝postgresql,默認會建一個名為”postgres”的系統賬號,用于執行PostgreSQL; [root@psql_master ~]# su - postgres #切換用戶后,提示符變更為“-bash-4.2$”; #同時資料庫中也會生成一個名為”postgres”的資料庫用戶,且密碼已自動生成; #PostgreSQL在資料庫用戶同名的系統賬號下登錄免密; -bash-4.2$ psql -U postgres #進入資料庫后修改密碼; postgres=# alter user postgres with password 'postgres@123'

- 允許遠程訪問
#組態檔中,默認只能本機訪問postgresql; #修改listen_addresses = 'localhost'為listen_addresses = '*',允許所有遠程訪問; #修改組態檔需要重啟服務, [root@psql_master ~]# sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /var/lib/pgsql/9.6/data/postgresql.conf - 主機認證
#在第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檔案需要重啟服務, [root@psql_master ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf host all all 172.29.3.67/32 trust
- 設定環境變數
[root@psql_master ~]# vim /etc/profile export PATH=$PATH:/usr/pgsql-9.6/bin [root@psql_master ~]# source /etc/profile
- 重啟服務
[root@psql_master ~]# systemctl restart postgresql-9.6
- 防火墻添加5432埠
開埠命令: firewall-cmd --zone=public --add-port=5432/tcp --permanent 重啟防火墻:systemctl restart firewalld.service
4、測驗
- 查看埠
[root@psql_master ~]# netstat -tunlp

- 創建用戶
postgres=# create user postuser1 with password 'user1@123';
- 創建資料庫
postgres=# create database postdb1 owner postuser1; 同時制定資料庫所有者
- 資料庫賦權
postgres=# grant all privileges on database postdb1 to postuser1 #未賦權則賬戶只能登錄控制臺
- 登錄新建資料庫
#在作業系統層使用新建的賬號登錄新建的資料庫,登錄后提示符為“postdb1=>”; #如果在postgres賬戶下直接使用“postgres=# \c postdb1;”登錄,則登錄用戶依然是postgres, -bash-4.2$ psql -U postuser1 -d postdb1 -h 127.0.0.1 -p 5432
- 創建表
postdb1=> create table tb1( id int primary key, name VARCHAR(20), salary real ); - 插入資料
postdb1=> insert into tb1( id, name, salary) values( 101, 'Mike', 5000.00 ); - 查詢
postdb1=>select * from tb1;

5、pgadmin連接postgresql
- pgadmin下載地址:https://www.pgadmin.org/download/
- 添加服務器
打開pgadmin—>添加新的服務器—>(通常標簽)名稱自定義—>(connection標簽)主機名稱與postgresql用戶密碼按需填寫,其余可采用默認配置—>保存


- 圖形化查看

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/209595.html
標籤:其他
