PG學習初體驗--原始碼安裝和簡單命令

jeanron100發表於2016-05-12
   其實對於PG,自己總是聽圈內人說和Oracle很相似,自己也有一些蠢蠢欲動學習的想法,從我的感覺來看,它是介於Oracle和MySQL之間的一種資料庫,具備類似Oracle的功能,兼具MySQL的開源風格。所以個人感覺無論是從Oracle轉向學習PG,還是從MySQL轉向PG都會有一些獨到的側重方向。當然學習資料庫本身不是目的,會的越多並不能說明你很牛,能夠深入理解資料庫,就如同感受的性格和處事風格,在合適的場景使用它,無想應該是超越於技術本身之外,而且難能可貴的。
  其實我本身也是一個浮躁的人,不喜歡全表掃描式的學習,很多東西都不喜歡按照那種系統的方式來學習,很多東西都想先問問,或者走捷徑最好。如果一個坎繞過去了,我喜歡再繞回去反覆走走。所以在快下班的時候,專門抽了不到一個小時的時間,在同事的幫助下完成了PG的安裝。
  當然本來是想簡單安裝一下PG,簡單瞭解一下,結果最後竟然嘗試成功了原始碼安裝。
如果網路允許,完全可以使用wget來下載,或者到官網離線下載。
#wget
。。。
HTTP request sent, awaiting response... 200 OK
Length: 24100449 (23M) [application/x-gzip]
Saving to: `postgresql-9.5.2.tar.gz'

100%[========================>] 24,100,449  4.82M/s   in 7.0s   
2016-05-12 17:30:52 (3.26 MB/s) - `postgresql-9.5.2.tar.gz' saved [24100449/24100449]
得到了原始碼包之後,使用tar -zxvf的方式解壓即可。
至於原始碼安裝,真心比自己想象要簡單很多,不過也遇到了一些小問題。
原始碼安裝的步驟就是./configure  ; gmake ; gmake install三步
在第一步的時候發現有這麼個錯誤。
checking for library containing sched_yield... none required
checking for library containing gethostbyname_r... none required
checking for library containing shmget... none required
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
看起來是readline的包缺少,但是檢視rpm包是有的。
#rpm -qa|grep readli
readline-5.1-3.el5
readline-5.1-3.el5
還有一個是zlib包的同樣警告,最後勉強使用./configure --without-readline --without-zlib的方式編譯,終於成功了。當然這種方式會無法啟用一些特性,readline我是知道的,應該是在命令中上下翻頁的功能會失效。但是暫時不影響核心功能。
gmake; gmake install的步驟很快就完成了,最後以一句“PostgreSQL installation complete.”結束。

接下來就是建立使用者,預設還是建立postgres的使用者,要不可能要改動一些配置檔案。
useradd postgres
然後把/usr/local/psql/bin放入環境變數中。
$which initdb
/usr/local/pgsql/bin/initdb
使用下面的方式來初始化,這個過程就如同MySQL的installdb一般。
$initdb -D /home/postgres/data --locale=C --encoding=UTF8
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

然後使用下面的方式來啟動PG,當然可以配置到service中,可以使用pg_ctl來啟動,方法確實比較多。
$postgres -D /home/postgres/data >/home/postgres/log/pg.log &2>1
完成之後就可以順利進入PG的命令列介面了。
當然readline的功能確實是無法啟用,我們回過頭來看看到底是怎麼回事。經過簡單的排查,認為是缺少了readline-devel的包,使用yum來安裝後。我們來清空編譯,重新編譯一次。
make clean
./configure
gmake
gmake install
當然這一次就非常順利了,很快就進入了PG命令列介面。
我們來簡單看看PG的程式,可以看到它也是有著多程式的方式,裡面尤其是write process,checkpointer  process和Oracle中應該是類似的功能。
[postgres@iZu127ehmv7Z ~]$ps -ef|grep post
root     12928 24641  0 18:07 pts/0    00:00:00 su - postgres
postgres 12929 12928  0 18:07 pts/0    00:00:00 -bash
postgres 12953 12929  0 18:07 pts/0    00:00:00 postgres -D /home/postgres/data
postgres 12955 12953  0 18:07 ?        00:00:00 postgres: checkpointer process   
postgres 12956 12953  0 18:07 ?        00:00:00 postgres: writer process       
postgres 12957 12953  0 18:07 ?        00:00:00 postgres: wal writer process   
postgres 12958 12953  0 18:07 ?        00:00:00 postgres: autovacuum launcher process   
postgres 12959 12953  0 18:07 ?        00:00:00 postgres: stats collector process   
postgres 13020 12929  0 18:36 pts/0    00:00:00 ps -ef
postgres 13021 12929  0 18:36 pts/0    00:00:00 grep post

我們來簡單用幾個命令玩玩。
檢視PG的版本
postgres=# SELECT version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54), 64-bit
(1 row)
檢視連線資訊。
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
檢視資料字典,看看有哪些資料庫。
postgres=# select datname from pg_database;
  datname  
-----------
 template1
 template0
 postgres
 test
(4 rows)
想看看使用的使用者
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
看看當前時間,這些和MySQL是一樣的。
postgres=# select now();
              now              
-------------------------------
 2016-05-12 19:44:35.337461+08
(1 row)
看看有哪些schemas
postgres=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)
建立一個資料庫test,方式和MySQL一樣。
create database test;
檢視資料庫的大小,可以發現是大小寫敏感的。
STATEMENT:  SELECT pg_size_pretty(pg_database_size('TEST')) As fulldbsize;
ERROR:  database "TEST" does not exist
postgres=#   SELECT pg_size_pretty(pg_database_size('test')) As fulldbsize;
 fulldbsize
------------
 7224 kB
(1 row)
如果要連入test資料庫
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=#
檢視存在的資料庫
test=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     |
(4 rows)
檢視資料字典的資訊
postgres=#  \dS+
                                     List of relations
   Schema   |              Name               | Type  |  Owner   |    Size    | Description
------------+---------------------------------+-------+----------+------------+-------------
 pg_catalog | pg_aggregate                    | table | postgres | 48 kB      |
 pg_catalog | pg_am                           | table | postgres | 40 kB      |
 pg_catalog | pg_amop                         | table | postgres | 80 kB      |
 pg_catalog | pg_amproc                       | table | postgres | 64 kB      |
 pg_catalog | pg_attrdef                      | table | postgres | 8192 bytes |
 pg_catalog | pg_attribute                    | table | postgres | 392 kB     |
 pg_catalog | pg_auth_members                 | table | postgres | 0 bytes    |
 pg_catalog | pg_authid                       | table | postgres | 40 kB      |

比如我們來看看pg_settings的欄位情況,嘗試使用desc
postgres=# desc pg_settings
postgres-#
竟然沒有任何反應,原來是用\d的方式
postgres=# \d pg_settings
     View "pg_catalog.pg_settings"
     Column      |  Type   | Modifiers
-----------------+---------+-----------
 name            | text    |
 setting         | text    |
 unit            | text    |
 category        | text    |
。。。
檢視最近執行的命令
\s
忍不住建立一個表試試
test=# create table test_tab(id int);
CREATE TABLE
從資料字典裡檢視錶的資訊
test=# select table_catalog,table_schema,table_name,table_type from information_schema.tables;
 table_catalog |    table_schema    |              table_name               | table_type
---------------+--------------------+---------------------------------------+------------
 test          | public             | test_tab                              | BASE TABLE
 test          | pg_catalog         | pg_statistic                          | BASE TABLE
 test          | pg_catalog         | pg_type                               | BASE TABLE
 test          | pg_catalog         | pg_authid                             | BASE TABLE
 test          | pg_catalog         | pg_roles                              | VIEW
 test          | pg_catalog         | pg_shadow                             | VIEW
檢視錶中欄位的情況
test=# SELECT column_name FROM information_schema.columns WHERE table_name ='test_tab';
 column_name
-------------
 id
(1 row)
檢視資料庫test中的表
test=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | test_tab | table | postgres
(1 row)






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23718752/viewspace-2098920/,如需轉載,請註明出處,否則將追究法律責任。

相關文章