1. 建立使用者 testuser 及密碼
# su – postgres
$ /bin/createuser -P testuser
輸入新身份的密碼:
再輸入一次:
2. 刪除使用者
$ /bin/dropuser testuser[@more@]
3. 建立資料庫 testdb,並指定管理者 testuser
$ /bin/createdb -O testuser testdb
4. 進行連線
$ psql -U testuser testdb
用戶 testuser 的密碼:
psql (9.2.18)
輸入 “help” 顯示說明。
testdb=>help
您正在使用 PostgreSQL 指令列介面 psql。
輸入: copyright 顯示發行條款
h 顯示 SQL 指令的說明
? 顯示 psql 指令的說明
g 或者以分號 (;) 結尾以執行查詢
q 結束
5. 建立資料表
testdb=> create table users (
testdb(> name varchar(30),
testdb(> age integer,
testdb(> mobiletel varchar(10),
testdb(> address varchar(80));
CREATE TABLE
6. 插入資料
testdb=> insert into users values (‘陳小華’,31,’0910001002′,’頭城鎮開蘭路123號’);
INSERT 0 1
testdb=> insert into users values (‘吳小剛’,38,’0910003004′,’頭城鎮開蘭路456號’);
INSERT 0 1
7. 瀏覽資料
testdb=> select * from users;
name | age | mobiletel | address
——–+—–+————+——————-
陳小華 | 31 | 0910001002 | 頭城鎮開蘭路123號
吳小剛 | 38 | 0910003004 | 頭城鎮開蘭路456號
(2 筆資料列)
testdb=> select * from users where age=31;
name | age | mobiletel | address
——–+—–+————+——————-
陳小華 | 31 | 0910001002 | 頭城鎮開蘭路123號
testdb=> select * from users where name=’吳小剛’;
name | age | mobiletel | address
——–+—–+————+——————-
吳小剛 | 38 | 0910003004 | 頭城鎮開蘭路456號
testdb=> update users set mobiletel=’0910005006′ where name=’吳小剛’;
UPDATE 1
testdb=> select * from users where name=’吳小剛’;
name | age | mobiletel | address
——–+—–+————+——————-
吳小剛 | 38 | 0910005006 | 頭城鎮開蘭路456號
testdb=> delete from users where name=’吳小剛’;
DELETE 1
testdb=> select * from users;
name | age | mobiletel | address
——–+—–+————+——————-
陳小華 | 31 | 0910001002 | 頭城鎮開蘭路123號
8. 刪除資料表
testdb=> drop table users;
DROP TABLE
9.
testdb=> l
資料庫清單
名稱 | 擁有者 | 字元編碼 | Collate | 轉換型別 | 存取權限
———–+———-+———-+————-+————-+———————–
postgres | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 |
template0 | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | testuser | UTF8 | zh_TW.UTF-8 | zh_TW.UTF-8 |
(4 筆資料列)
testdb=> du
角色清單
角色名稱 | 屬性 | 成員屬於
———-+———————————–+———-
postgres | 超級用戶, 建立角色, 建立 DB, 複製 | {}
testuser | | {}
命令列操作
# sudo -u postgres psql -c “CREATE USER testuser WITH PASSWORD ‘密碼’;”
# sudo -u postgres psql -c “CREATE DATABASE testdb;”
# sudo -u postgres psql -c “GRANT ALL PRIVILEGES ON DATABASE testdb to testuser;”
# sudo -u postgres psql -c “ALTER USER testuser WITH SUPERUSER;”
# sudo -u postgres psql -c “ALTER ROLE testuser NOSUPERUSER;”
# sudo -u postgres psql -c “ALTER ROLE testuser NOCREATEDB;”
$ /bin/pg_dump -f users_dump.sql -U testuser -W testdb
$ /bin/psql -U testuser testdb -f users_dump.sql