前幾篇文章介紹了GreenPlum數據庫的安裝、啟動、關閉、狀態檢查、登錄等操作,數據庫已經創建好了,接下來介紹如何使用數據庫。按照習慣,需要先創建測試用戶、表空間、數據庫。先創建測試用戶dbdream。
2 |
NOTICE: resource queue required -- using default resource queue "pg_default" |
通常創建用戶,賦予login權限基本就夠用了,創建用戶的語法可以通過\h create role命令來查看,這里不做記錄了。可以通過pg_roles字典開查看數據庫的用戶信息。
創建完用戶后,還需要修改pg_hba.conf文件,來賦予用戶的遠程登錄權限。
1 |
[gpadmin@mdw gpseg-1]$ vi pg_hba.conf |
3 |
host all dbdream 10.9.15.20/32 md5 |
通過gpstop –u命令重新加載配置文件,使之生效后,dbdream用戶即可遠程訪問數據庫了。
1 |
[gpadmin@mdw gpseg-1]$ psql -d postgres -U dbdream -h 10.9.15.20 -p 5432 |
2 |
Password for user dbdream: |
可以使用\c命令或者\conninfo命令來查看當前的登錄信息。
2 |
You are now connected to database "postgres" as user "dbdream". |
創建表空間需要先創建文件空間,文件空間是在操作系統層創建,創建文件控制需要指定已存在的目錄,通常需要規划並創建目錄。Master和所有Segment節點都需要創建。可以使用下面的命令查看當前數據庫的表空間和文件空間對應關系。
3 |
------------+----------- |
當前數據庫只有pg_segment一個文件空間,上面有pg_default和pg_global兩個表空間 。下面先創建Master節點的目錄。
1 |
[gpadmin@mdw ~]$ mkdir -p /gpdb/gpdata/fspc_master |
然后在Master節點通過gpssh命令創建Segment節點的目錄。
1 |
[gpadmin@mdw ~]$ gpssh -f /gpdb/app/config/seg_host -e 'mkdir -p /gpdb/gpdata/fspc_segment' |
2 |
[sdw1] mkdir -p /gpdb/gpdata/fspc_segment |
3 |
[sdw2] mkdir -p /gpdb/gpdata/fspc_segment |
4 |
[sdw3] mkdir -p /gpdb/gpdata/fspc_segment |
創建完目錄后,可以通過gpfilespace命令創建文件空間。
1 |
gpadmin@mdw gpdata]$ gpfilespace |
2 |
20160116:11:25:31:004066 gpfilespace:mdw:gpadmin-[INFO]:- |
3 |
A tablespace requires a file system location to store its database |
4 |
files. A filespace is a collection of file system locations for all components |
5 |
in a Greenplum system (primary segment, mirror segment and master instances). |
6 |
Once a filespace is created, it can be used by one or more tablespaces. |
8 |
20160116:11:25:31:004066 gpfilespace:mdw:gpadmin-[INFO]:-getting config |
9 |
Enter a name for this filespace |
輸入表空間的名字,名字隨便起,但是不能使用gp_開頭。
03 |
Checking your configuration: |
04 |
Your system has 1 hosts with 0 primary and 0 mirror segments per host. |
05 |
Your system has 3 hosts with 1 primary and 0 mirror segments per host. |
07 |
Configuring hosts: [mdw] |
09 |
Configuring hosts: [sdw1, sdw2, sdw3] |
11 |
Please specify 1 locations for the primary segments, one per line: |
這里輸入Segment節點的文件空間目錄。
1 |
primary location 1> /gpdb/gpdata/fspc_segment |
3 |
Enter a file system location for the master |
這里輸入Master幾點的文件空間目錄。
1 |
master location> /gpdb/gpdata/fspc_master |
2 |
20160116:11:27:21:004066 gpfilespace:mdw:gpadmin-[INFO]:-Creating configuration file... |
3 |
20160116:11:27:21:004066 gpfilespace:mdw:gpadmin-[INFO]:-[created] |
4 |
20160116:11:27:21:004066 gpfilespace:mdw:gpadmin-[INFO]:- |
5 |
To add this filespace to the database please run the command: |
6 |
gpfilespace --config /gpdb/gpdata/gpfilespace_config_20160116_112531 |
gpfilespace命令執行完成后,會生成一個配置文件,也就是上面的最后一行,/gpdb/gpdata/gpfilespace_config_20160116_112531,查看這個文件的內容會發現,上面的gpfilespace命令其實只是創建了文件空間的配置信息,並沒有真正創建文件空間。
1 |
[gpadmin@mdw gpdata]$ cat /gpdb/gpdata/gpfilespace_config_20160116_112531 |
3 |
mdw:1:/gpdb/gpdata/fspc_master/gpseg-1 |
4 |
sdw1:2:/gpdb/gpdata/fspc_segment/gpseg0 |
5 |
sdw2:3:/gpdb/gpdata/fspc_segment/gpseg1 |
6 |
sdw3:4:/gpdb/gpdata/fspc_segment/gpseg2 |
這個文件可以自己創建並修改,真正創建文件空間需要運行剛才gpfilespace命令后的最后一行。
01 |
gpadmin@mdw gpdata]$ gpfilespace --config /gpdb/gpdata/gpfilespace_config_20160116_112531 |
02 |
20160116:11:29:19:004180 gpfilespace:mdw:gpadmin-[INFO]:- |
03 |
A tablespace requires a file system location to store its database |
04 |
files. A filespace is a collection of file system locations for all components |
05 |
in a Greenplum system (primary segment, mirror segment and master instances). |
06 |
Once a filespace is created, it can be used by one or more tablespaces. |
08 |
20160116:11:29:19:004180 gpfilespace:mdw:gpadmin-[INFO]:-getting config |
09 |
Reading Configuration file: '/gpdb/gpdata/gpfilespace_config_20160116_112531' |
10 |
20160116:11:29:19:004180 gpfilespace:mdw:gpadmin-[INFO]:-Performing validation on paths |
11 |
.............................................................................. |
12 |
20160116:11:29:19:004180 gpfilespace:mdw:gpadmin-[INFO]:-Connecting to database |
13 |
20160116:11:29:19:004180 gpfilespace:mdw:gpadmin-[INFO]:-Filespace "fspc1" successfully created |
這樣才是真正創建完成文件空間,才能在數據庫中查詢到新建的文件空間信息。
創建完文件空間,即可在文件空間上創建表空間,創建表空間必須使用support權限用戶。
1 |
postgres-> \c postgres gpadmin |
2 |
You are now connected to database "postgres" as user "gpadmin". |
非support權限用戶要使用新建的表空間,必須要使用support用戶對其授予操作權限才可以,否則會遇到下面的錯誤。
2 |
Password for user dbdream: |
3 |
You are now connected to database "dbdream" as user "dbdream". |
5 |
dbdream=> create table t_test(id int,name varchar(10)) DISTRIBUTED BY(id); |
6 |
ERROR: permission denied for tablespace tbs1 |
要使用戶默認就使用新建的表空間,需要設置用戶的默認表空間。
授權后,即可使用新建的表空間。
01 |
postgres-> \c postgres gpadmin |
02 |
You are now connected to database "postgres" as user "gpadmin". |
07 |
dbdream=> create table t_test(id int,name varchar(10)) DISTRIBUTED BY(id); |
11 |
Column | Type | Modifiers |
12 |
--------+-----------------------+----------- |
14 |
name | character varying(10) | |
具有createdb權限的用戶,都可以創建數據庫了,語法可以通過\h create database命令來查看,非常簡單。比如下面這條命令就可以創建一個數據庫。
1 |
postgres=> create database dbdream; |
也可以在創建數據庫時,指定數據庫使用的默認表空間。
創建數據庫的用戶必須要有createdb權限或者support用戶權限,這也是之前創建用戶是為什么直接賦予了createdb權限,可以通過pg_database字典來查看數據庫信息。
2 |
datname | datdba | dattablespace | |
3 |
-----------+--------+----------------+ |
4 |
dbdream | 17197 | 1663 | |
7 |
template1 | 10 | 1663 | |
8 |
template0 | 10 | 1663 | |
datdba字段表示的是數據庫的所有者(創建者),這里存的是用戶的oid,10是gpadmin用戶,17191是dbdream用戶,可以通過pg_role字典來查看。dattablespace字段表示的是表空間。存的也是表空間的ID,1663是pg_default表空間,17199是新建的tbs1表空間,可以通過pg_tablespace字典開查看。
本文固定鏈接: http://www.dbdream.com.cn/2016/01/greenplum%e6%95%b0%e6%8d%ae%e5%ba%93%e5%88%9b%e5%bb%ba%e7%94%a8%e6%88%b7%e3%80%81%e6%96%87%e4%bb%b6%e7%a9%ba%e9%97%b4%e3%80%81%e8%a1%a8%e7%a9%ba%e9%97%b4%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93/ | 信春哥,系統穩,閉眼上線不回滾!