可以參考:https://www.cnblogs.com/freeweb/p/8006639.html#top
https://www.cnblogs.com/EasonJim/p/9057867.html
我這里使用的rpm方式,參考鏈接使用的是tar.gz的方式,看個人需求的吧,沒有網絡可以使用tar.gz下載好進行安裝。
1、Linux查看版本當前操作系統發行信:cat /etc/centos-release。
1 [root@slaver1 ~]# cat /etc/centos-release 2 CentOS Linux release 7.7.1908 (Core) 3 [root@slaver1 ~]#
2、Linux查看版本當前操作系統內核信息:uname -a。
1 [root@slaver1 ~]# uname -a 2 Linux slaver1 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 3 [root@slaver1 ~]#
3、PostgreSQL官網https://www.postgresql.org/
選擇自己是什么操作系統, 然后選擇對應得即可。
我的是Centos操作系統,如下所示:
然后版本,操作系統版本,多少位的。
4、執行yum下載命令,將rpm包下載下來,如下所示:
1 [root@slaver1 package]# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm 2 已加載插件:fastestmirror, langpacks 3 pgdg-redhat-repo-latest.noarch.rpm | 6.5 kB 00:00:00 4 正在檢查 /var/tmp/yum-root-OuHt4M/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-11.noarch 5 /var/tmp/yum-root-OuHt4M/pgdg-redhat-repo-latest.noarch.rpm 將被安裝 6 正在解決依賴關系 7 --> 正在檢查事務 8 ---> 軟件包 pgdg-redhat-repo.noarch.0.42.0-11 將被 安裝 9 --> 解決依賴關系完成 10 11 依賴關系解決 12 13 ================================================================================================================================================================================================================================ 14 Package 架構 版本 源 大小 15 ================================================================================================================================================================================================================================ 16 正在安裝: 17 pgdg-redhat-repo noarch 42.0-11 /pgdg-redhat-repo-latest.noarch 11 k 18 19 事務概要 20 ================================================================================================================================================================================================================================ 21 安裝 1 軟件包 22 23 總計:11 k 24 安裝大小:11 k 25 Is this ok [y/d/N]: y 26 Downloading packages: 27 Running transaction check 28 Running transaction test 29 Transaction test succeeded 30 Running transaction 31 正在安裝 : pgdg-redhat-repo-42.0-11.noarch 1/1 32 驗證中 : pgdg-redhat-repo-42.0-11.noarch 1/1 33 34 已安裝: 35 pgdg-redhat-repo.noarch 0:42.0-11 36 37 完畢!
5、然后開始安裝postgresql服務器端,如下所示:
1 [root@slaver1 package]# yum install postgresql10-server 2 已加載插件:fastestmirror, langpacks 3 /var/run/yum.pid 已被鎖定,PID 為 78607 的另一個程序正在運行。 4 Another app is currently holding the yum lock; waiting for it to exit... 5 另一個應用程序是:PackageKit 6 內存: 31 M RSS (451 MB VSZ) 7 已啟動: Thu Aug 13 09:51:43 2020 - 00:11之前 8 狀態 :睡眠中,進程ID:78607 9 Another app is currently holding the yum lock; waiting for it to exit... 10 另一個應用程序是:PackageKit 11 內存: 32 M RSS (452 MB VSZ) 12 已啟動: Thu Aug 13 09:51:43 2020 - 00:13之前 13 狀態 :睡眠中,進程ID:78607 14 Another app is currently holding the yum lock; waiting for it to exit... 15 另一個應用程序是:PackageKit 16 內存: 32 M RSS (452 MB VSZ) 17 已啟動: Thu Aug 13 09:51:43 2020 - 00:15之前 18 狀態 :睡眠中,進程ID:78607 19 Another app is currently holding the yum lock; waiting for it to exit... 20 另一個應用程序是:PackageKit 21 內存: 32 M RSS (452 MB VSZ) 22 已啟動: Thu Aug 13 09:51:43 2020 - 00:17之前 23 狀態 :睡眠中,進程ID:78607 24 Another app is currently holding the yum lock; waiting for it to exit... 25 另一個應用程序是:PackageKit 26 內存: 32 M RSS (452 MB VSZ) 27 已啟動: Thu Aug 13 09:51:43 2020 - 00:19之前 28 狀態 :睡眠中,進程ID:78607 29 Another app is currently holding the yum lock; waiting for it to exit... 30 另一個應用程序是:PackageKit 31 內存: 32 M RSS (452 MB VSZ) 32 已啟動: Thu Aug 13 09:51:43 2020 - 00:21之前 33 狀態 :睡眠中,進程ID:78607 34 Another app is currently holding the yum lock; waiting for it to exit... 35 另一個應用程序是:PackageKit 36 內存: 38 M RSS (459 MB VSZ) 37 已啟動: Thu Aug 13 09:51:43 2020 - 00:23之前 38 狀態 :運行中,進程ID:78607 39 Another app is currently holding the yum lock; waiting for it to exit... 40 另一個應用程序是:PackageKit 41 內存:174 M RSS (594 MB VSZ) 42 已啟動: Thu Aug 13 09:51:43 2020 - 00:25之前 43 狀態 :運行中,進程ID:78607 44 Another app is currently holding the yum lock; waiting for it to exit... 45 另一個應用程序是:PackageKit 46 內存:265 M RSS (685 MB VSZ) 47 已啟動: Thu Aug 13 09:51:43 2020 - 00:27之前 48 狀態 :運行中,進程ID:78607 49 Another app is currently holding the yum lock; waiting for it to exit... 50 另一個應用程序是:PackageKit 51 內存:265 M RSS (685 MB VSZ) 52 已啟動: Thu Aug 13 09:51:43 2020 - 00:29之前 53 狀態 :睡眠中,進程ID:78607 54 Another app is currently holding the yum lock; waiting for it to exit... 55 另一個應用程序是:PackageKit 56 內存:265 M RSS (685 MB VSZ) 57 已啟動: Thu Aug 13 09:51:43 2020 - 00:31之前 58 狀態 :睡眠中,進程ID:78607 59 Loading mirror speeds from cached hostfile 60 * base: mirrors.aliyun.com 61 * epel: mirrors.yun-idc.com 62 * extras: mirror.bit.edu.cn 63 * updates: mirrors.aliyun.com 64 正在解決依賴關系 65 --> 正在檢查事務 66 ---> 軟件包 postgresql10-server.x86_64.0.10.13-1PGDG.rhel7 將被 安裝 67 --> 正在處理依賴關系 postgresql10-libs(x86-64) = 10.13-1PGDG.rhel7,它被軟件包 postgresql10-server-10.13-1PGDG.rhel7.x86_64 需要 68 --> 正在處理依賴關系 postgresql10(x86-64) = 10.13-1PGDG.rhel7,它被軟件包 postgresql10-server-10.13-1PGDG.rhel7.x86_64 需要 69 --> 正在處理依賴關系 libpq.so.5()(64bit),它被軟件包 postgresql10-server-10.13-1PGDG.rhel7.x86_64 需要 70 --> 正在檢查事務 71 ---> 軟件包 postgresql10.x86_64.0.10.13-1PGDG.rhel7 將被 安裝 72 ---> 軟件包 postgresql10-libs.x86_64.0.10.13-1PGDG.rhel7 將被 安裝 73 --> 解決依賴關系完成 74 75 依賴關系解決 76 77 ================================================================================================================================================================================================================================ 78 Package 架構 版本 源 大小 79 ================================================================================================================================================================================================================================ 80 正在安裝: 81 postgresql10-server x86_64 10.13-1PGDG.rhel7 pgdg10 4.5 M 82 為依賴而安裝: 83 postgresql10 x86_64 10.13-1PGDG.rhel7 pgdg10 1.6 M 84 postgresql10-libs x86_64 10.13-1PGDG.rhel7 pgdg10 357 k 85 86 事務概要 87 ================================================================================================================================================================================================================================ 88 安裝 1 軟件包 (+2 依賴軟件包) 89 90 總下載量:6.5 M 91 安裝大小:28 M 92 Is this ok [y/d/N]: y 93 Downloading packages: 94 警告:/var/cache/yum/x86_64/7/pgdg10/packages/postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID 442df0f8: NOKEY ] 128 kB/s | 416 kB 00:00:48 ETA 95 postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm 的公鑰尚未安裝 96 (1/3): postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm | 357 kB 00:00:05 97 (2/3): postgresql10-10.13-1PGDG.rhel7.x86_64.rpm | 1.6 MB 00:02:40 98 (3/3): postgresql10-server-10.13-1PGDG.rhel7.x86_64.rpm | 4.5 MB 00:02:44 99 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 100 總計 39 kB/s | 6.5 MB 00:02:50 101 從 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG 檢索密鑰 102 導入 GPG key 0x442DF0F8: 103 用戶ID : "PostgreSQL RPM Building Project <pgsqlrpms-hackers@pgfoundry.org>" 104 指紋 : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8 105 軟件包 : pgdg-redhat-repo-42.0-11.noarch (@/pgdg-redhat-repo-latest.noarch) 106 來自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG 107 是否繼續?[y/N]:y 108 Running transaction check 109 Running transaction test 110 Transaction test succeeded 111 Running transaction 112 正在安裝 : postgresql10-libs-10.13-1PGDG.rhel7.x86_64 1/3 113 正在安裝 : postgresql10-10.13-1PGDG.rhel7.x86_64 2/3 114 正在安裝 : postgresql10-server-10.13-1PGDG.rhel7.x86_64 3/3 115 驗證中 : postgresql10-libs-10.13-1PGDG.rhel7.x86_64 1/3 116 驗證中 : postgresql10-10.13-1PGDG.rhel7.x86_64 2/3 117 驗證中 : postgresql10-server-10.13-1PGDG.rhel7.x86_64 3/3 118 119 已安裝: 120 postgresql10-server.x86_64 0:10.13-1PGDG.rhel7 121 122 作為依賴被安裝: 123 postgresql10.x86_64 0:10.13-1PGDG.rhel7 postgresql10-libs.x86_64 0:10.13-1PGDG.rhel7 124 125 完畢! 126 [root@slaver1 package]#
6、開始初始化postgresql數據庫,如下所示:
1 [root@slaver1 package]# /usr/pgsql-10/bin/postgresql-10-setup initdb 2 Initializing database ... OK 3 4 [root@slaver1 package]#
7、設置開機自啟動,然后啟動postgresql數據庫,如下所示:
1 [root@slaver1 package]# systemctl enable postgresql-10 2 Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service. 3 [root@slaver1 package]# systemctl start postgresql-10 4 [root@slaver1 package]#
8、查看postgresql的相關進程,如下所示:
1 [root@slaver1 ~]# ps -ef | grep postgres 2 postgres 79928 1 0 10:07 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/ 3 postgres 79931 79928 0 10:07 ? 00:00:00 postgres: logger process 4 postgres 79933 79928 0 10:07 ? 00:00:00 postgres: checkpointer process 5 postgres 79934 79928 0 10:07 ? 00:00:00 postgres: writer process 6 postgres 79935 79928 0 10:07 ? 00:00:00 postgres: wal writer process 7 postgres 79936 79928 0 10:07 ? 00:00:00 postgres: autovacuum launcher process 8 postgres 79937 79928 0 10:07 ? 00:00:00 postgres: stats collector process 9 postgres 79938 79928 0 10:07 ? 00:00:00 postgres: bgworker: logical replication launcher 10 root 84515 78910 0 10:56 pts/4 00:00:00 grep --color=auto postgres 11 [root@slaver1 ~]#
pgsql默認的端口號為5432,通過netstat命令或者lsof命令都可以看到監聽情況:
1 [root@slaver1 ~]# lsof -i:5432 2 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 3 postmaste 79928 postgres 3u IPv6 663083 0t0 TCP localhost:postgres (LISTEN) 4 postmaste 79928 postgres 4u IPv4 663084 0t0 TCP localhost:postgres (LISTEN) 5 [root@slaver1 ~]# netstat -an | grep 5432 6 tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 7 tcp6 0 0 ::1:5432 :::* LISTEN 8 unix 2 [ ACC ] STREAM LISTENING 663093 /var/run/postgresql/.s.PGSQL.5432 9 unix 2 [ ACC ] STREAM LISTENING 663095 /tmp/.s.PGSQL.5432 10 [root@slaver1 ~]#
現在可以看到pgsql默認監聽的是localhost或127.0.0.1,目前只能本機訪問,如果遠程訪問就連接不上了,具體的配置文件是data目錄下的postgresql.conf,可以通過修改這個配置文件來調整各個參數,比如:listen_addresses可以修改綁定的地址,默認是localhost,port可以修改監聽的端口號,默認是5432,max_connections可以修改最大客戶端連接數量,默認是100等等。
注意:我這里才用的是默認的安裝方式,就使用find命令查找了一下postgresql.conf的配置位置,然后進行修改:
1 [root@slaver1 /]# find -name postgresql.conf 2 ./var/lib/pgsql/10/data/postgresql.conf 3 ./usr/lib/tmpfiles.d/postgresql.conf 4 [root@slaver1 /]# ll /var/lib/pgsql/10/data/ 5 總用量 60 6 drwx------. 5 postgres postgres 41 8月 13 10:06 base 7 -rw-------. 1 postgres postgres 30 8月 13 10:07 current_logfiles 8 drwx------. 2 postgres postgres 4096 8月 13 10:20 global 9 drwx------. 2 postgres postgres 32 8月 13 10:07 log 10 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_commit_ts 11 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_dynshmem 12 -rw-------. 1 postgres postgres 4269 8月 13 10:06 pg_hba.conf 13 -rw-------. 1 postgres postgres 1636 8月 13 10:06 pg_ident.conf 14 drwx------. 4 postgres postgres 68 8月 13 10:12 pg_logical 15 drwx------. 4 postgres postgres 36 8月 13 10:06 pg_multixact 16 drwx------. 2 postgres postgres 18 8月 13 10:07 pg_notify 17 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_replslot 18 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_serial 19 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_snapshots 20 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_stat 21 drwx------. 2 postgres postgres 63 8月 13 11:01 pg_stat_tmp 22 drwx------. 2 postgres postgres 18 8月 13 10:06 pg_subtrans 23 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_tblspc 24 drwx------. 2 postgres postgres 6 8月 13 10:06 pg_twophase 25 -rw-------. 1 postgres postgres 3 8月 13 10:06 PG_VERSION 26 drwx------. 3 postgres postgres 60 8月 13 10:06 pg_wal 27 drwx------. 2 postgres postgres 18 8月 13 10:06 pg_xact 28 -rw-------. 1 postgres postgres 88 8月 13 10:06 postgresql.auto.conf 29 -rw-------. 1 postgres postgres 22985 8月 13 10:06 postgresql.conf 30 -rw-------. 1 postgres postgres 58 8月 13 10:07 postmaster.opts 31 -rw-------. 1 postgres postgres 104 8月 13 10:07 postmaster.pid 32 [root@slaver1 /]# vim /var/lib/pgsql/10/data/postgresql.conf 33 [root@slaver1 /]#
這里將localhost修改為192.168.110.133,如下所示:
9、命令行界面簡單操作,pgsql和mysql一樣可以通過交互式提示符連接操作,連接方式如下:
1 [root@slaver1 ~]# psql -h 127.0.0.1 -d postgres -U postgres -p 5432 2 psql: 致命錯誤: 用戶 "postgres" Ident 認證失敗 3 [root@slaver1 ~]#
修改認證文件[root@slaver1 ~]# vim /var/lib/pgsql/10/data/pg_hba.conf,登陸使用密碼。把這個配置文件中的認證 METHOD的ident修改為trust,可以實現用賬戶和密碼來訪問數據庫,即解決psql: 致命錯誤: 用戶 "postgres" Ident 認證失敗 這個問題)。
重啟postgresql服務器使設置生效,如下所示:
注意:其中-h參數指定服務器地址,默認為127.0.0.1,默認不指定即可,-d指定連接之后選中的數據庫,默認也是postgres,-U指定用戶,默認是當前用戶,-p 指定端口號,默認是"5432",其它更多的參數選項可以執行: ./bin/psql --help 查看。
1 [root@slaver1 ~]# psql -h 127.0.0.1 -d postgres -U postgres -p 5432 2 psql: could not connect to server: 拒絕連接 3 Is the server running on host "127.0.0.1" and accepting 4 TCP/IP connections on port 5432? 5 [root@slaver1 ~]# psql -h 192.168.110.133 -d postgres -U postgres -p 5432 6 psql: 致命錯誤: 沒有用於主機 "192.168.110.133", 用戶 "postgres", 數據庫 "postgres", SSL 關閉 的 pg_hba.conf 記錄 7 [root@slaver1 ~]#
由於這里需要設置可以遠程鏈接,將127.0.0.1的都改成自己ip地址,vim /var/lib/pgsql/10/data/pg_hba.conf,如下所示:
修改完畢,重啟postgresql服務,如下所示:
1 [root@slaver1 ~]# systemctl stop postgresql-10 2 [root@slaver1 ~]# systemctl start postgresql-10 3 [root@slaver1 ~]# ps -ef | grep postgres 4 postgres 86970 1 2 11:23 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/ 5 postgres 86972 86970 0 11:23 ? 00:00:00 postgres: logger process 6 postgres 86974 86970 0 11:23 ? 00:00:00 postgres: checkpointer process 7 postgres 86975 86970 0 11:23 ? 00:00:00 postgres: writer process 8 postgres 86976 86970 0 11:23 ? 00:00:00 postgres: wal writer process 9 postgres 86977 86970 0 11:23 ? 00:00:00 postgres: autovacuum launcher process 10 postgres 86978 86970 0 11:23 ? 00:00:00 postgres: stats collector process 11 postgres 86979 86970 0 11:23 ? 00:00:00 postgres: bgworker: logical replication launcher 12 root 86982 78910 0 11:23 pts/4 00:00:00 grep --color=auto postgres 13 [root@slaver1 ~]# lsof -i:5432 14 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 15 postmaste 86970 postgres 3u IPv4 797866 0t0 TCP slaver1:postgres (LISTEN) 16 [root@slaver1 ~]# ps -ef | grep postgres 17 postgres 86970 1 0 11:23 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/ 18 postgres 86972 86970 0 11:23 ? 00:00:00 postgres: logger process 19 postgres 86974 86970 0 11:23 ? 00:00:00 postgres: checkpointer process 20 postgres 86975 86970 0 11:23 ? 00:00:00 postgres: writer process 21 postgres 86976 86970 0 11:23 ? 00:00:00 postgres: wal writer process 22 postgres 86977 86970 0 11:23 ? 00:00:00 postgres: autovacuum launcher process 23 postgres 86978 86970 0 11:23 ? 00:00:00 postgres: stats collector process 24 postgres 86979 86970 0 11:23 ? 00:00:00 postgres: bgworker: logical replication launcher 25 root 86994 78910 0 11:24 pts/4 00:00:00 grep --color=auto postgres 26 [root@slaver1 ~]#
然后使用命令行的方式連接postgresql數據庫,如下所示:
1 [root@slaver1 ~]# psql -h 192.168.110.133 -d postgres -U postgres -p 5432 2 psql (9.2.24, 服務器 10.13) 3 警告:psql 版本9.2, 服務器版本10.0. 4 一些psql功能可能無法工作. 5 輸入 "help" 來獲取幫助信息. 6 7 postgres=#
登錄進去默認界面,命令提示符前面的就是當前的數據庫,使用 \l 查看當前的數據庫列表,如下所示:
1 [root@slaver1 ~]# psql -h 192.168.110.133 -d postgres -U postgres -p 5432 2 psql (9.2.24, 服務器 10.13) 3 警告:psql 版本9.2, 服務器版本10.0. 4 一些psql功能可能無法工作. 5 輸入 "help" 來獲取幫助信息. 6 7 postgres=# 8 postgres=# \l 9 資料庫列表 10 名稱 | 擁有者 | 字元編碼 | 校對規則 | Ctype | 存取權限 11 -----------+----------+----------+-------------+-------------+----------------------- 12 postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | 13 template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + 14 | | | | | postgres=CTc/postgres 15 template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + 16 | | | | | postgres=CTc/postgres 17 (3 行記錄) 18 19 postgres=#
創建一個簡單的數據表可以執行下面的命令:
1 postgres=# CREATE TABLE student ( 2 postgres(# id integer NOT NULL, 3 postgres(# name character(32), 4 postgres(# number char(5), 5 postgres(# CONSTRAINT student_pkey PRIMARY KEY (id) 6 postgres(# ); 7 CREATE TABLE 8 postgres=# 9 postgres=#
創建表之后可以使用 \d student; 查看表的詳細信息,如下:
1 postgres=# \d student; 2 資料表 "public.student" 3 欄位 | 型別 | 修飾詞 4 --------+---------------+-------- 5 id | integer | 非空 6 name | character(32) | 7 number | character(5) | 8 索引: 9 "student_pkey" PRIMARY KEY, btree (id) 10 11 postgres=#
現在可以插入一條測試數據: INSERT INTO student (id, name, number) VALUES (1, '張三', '1023');
然后可以查詢這條數據: SELECT * FROM student WHERE id=1;
1 postgres=# INSERT INTO student (id, name, number) VALUES (1, '張三', '1023'); 2 INSERT 0 1 3 postgres=# SELECT * FROM student WHERE id=1; 4 id | name | number 5 ----+------------------------------------+-------- 6 1 | 張三 | 1023 7 (1 行記錄) 8 9 postgres=#
最后可以執行 \q 退出交互式界面。
1 postgres=# \q 2 [root@slaver1 ~]#
10、問題:致命錯誤: 用戶 "postgres" Ident 認證失敗?
說明:這個是由於沒有配置遠程訪問且認證方式沒改造成的,只需要更改使用賬號密碼認證即可。
解決:找到pg_hba.conf(find / -name pg_hba.conf),METHOD的ident修改為md5,且把ip端改為0.0.0.0/0,或者想要的IP地址即可。修改完成后直接reload即可。
11、METHOD指定如何處理客戶端的認證。常用的有ident,md5,password,trust,reject
1)、ident是Linux下PostgreSQL默認的local認證方式,凡是能正確登錄服務器的操作系統用戶(注:不是數據庫用戶)就能使用本用戶映射的數據庫用戶不需密碼登錄數據庫。用戶映射文件為pg_ident.conf,這個文件記錄着與操作系統用戶匹配的數據庫用戶,如果某操作系統用戶在本文件中沒有映射用戶,則默認的映射數據庫用戶與操作系統用戶同名。比如,服務器上有名為user1的操作系統用戶,同時數據庫上也有同名的數據庫用戶,user1登錄操作系統后可以直接輸入psql,以user1數據庫用戶身份登錄數據庫且不需密碼。很多初學者都會遇到psql -U username登錄數據庫卻出現“username ident 認證失敗”的錯誤,明明數據庫用戶已經createuser。原因就在於此,使用了ident認證方式,卻沒有同名的操作系統用戶或沒有相應的映射用戶。解決方案:第一種、在pg_ident.conf中添加映射用戶;第二種、改變認證方式。
2)、METHOD指定如何處理客戶端的認證,如下所示:
a、md5是常用的密碼認證方式,如果你不使用ident,最好使用md5。密碼是以md5形式傳送給數據庫,較安全,且不需建立同名的操作系統用戶。
b、password是以明文密碼傳送給數據庫,建議不要在生產環境中使用。
c、trust是只要知道數據庫用戶名就不需要密碼或ident就能登錄,建議不要在生產環境中使用。
d、reject是拒絕認證。
3)、在文件查找 listen_addresses,他的值說明:
a、如果希望只能從本地計算機訪問PostgreSQL數據庫,就將該項設置為'localhost';
b、如果希望從局域網訪問PostgreSQL數據庫,就將該項設置為PostgreSQL數據庫的局域網IP地址;
c、如果希望從互聯網訪問PostgreSQL數據庫,就將該項設置為PostgreSQL數據庫的互聯網IP地址;
d、如果希望從任何地方都可以訪問PostgreSQL數據庫,就將該配置項設置為“*”;
通過ident的描述可以看到上面出現錯誤(解決psql: 致命錯誤: 用戶 "postgres" Ident 認證失敗)的原因是什么了,是因為自己的linux的用戶沒有和postgresql匹配的用戶,才導致出現的錯誤的,其實這里根據上面的描述有好幾種解決方法,由於postgresql數據庫會默認生成一個名為postgres的數據庫和一個名為postgres的數據庫用戶,此時,可以同時生成了一個名為postgres的Linux系統用戶。但是又不能直接使用root賬號切換到postgresql賬號,所以需要額外創建一個linux用戶用來切換到postgresql賬號。
參考:http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html