1. 程式人生 > 實用技巧 >吳裕雄--天生自然ANDROID開發學習:2.5.0 構建一個可複用的自定義BaseAdapter

吳裕雄--天生自然ANDROID開發學習:2.5.0 構建一個可複用的自定義BaseAdapter

可以參考: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 <[email protected]>"
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