postgreSql 基本操做總結

0. 啓動pgsl數據庫

pg_ctl -D /xx/pgdata  start
 
 

1. 命令行登陸數據庫

1
psql -U username -d dbname -h hostip -p port
 

2. 列出全部數據庫

\l 
 

3. 切換數據庫

1
\c dbname
 

4. 列出當前數據庫的全部表

\d 

5. 查看指定表的全部字段

1
\d  tablename

6. 查看指定表的基本狀況

1
\d+  tablename

7. 退出操做

1
q

8. 新建表

例1(主鍵)css

複製代碼
create table TESTCASE(
id INTEGER, 
task_class INTEGER,
age TEXT,
PRIMARY KEY(id, task_class)
);
複製代碼

例2(自增SERIAL)html

create table CREATETASK_CHKID_N( 
id SERIAL PRIMARY KEY,
chk_id TEXT,
n INTEGER
);

其中SERIAL表明自增,默認從1開始增長,每次自增1。node

9. 刪除表

1
drop table REL_CROSS_NODE;

10. 清空表

delete from [表名]

orsql

TRUNCATE TABLE  [表名]

區別:Truncate table 表名 (注:不帶where語句) 速度快,並且效率高。數據庫

由於DELETE 語句每次刪除一行,並在事務日誌中爲所刪除的每行記錄一項。TRUNCATE TABLE 經過釋放存儲表數據所用的數據頁來刪除數據,而且只在事務日誌中記錄頁的釋放bash

11. 添加字段

1
alter table [表名] add column [字段名] [類型];

12. 更改字段

alter table [表名] rename column [舊字段名] to [新字段名];

例:把表table_ex字段col_1限制非空去掉:ALTER TABLE table_eg ALTER col_1 drop not NULL

12.1 更改字段屬性,含空格post

若是把字段colname把屬性Text轉化爲int,原來text裏面存在空啥的,能夠fetch

ALTER TABLE tablename ALTER COLUMN colname TYPE int USING (trim(colname)::integer);

12.2 更改字段由int4-->int8spa

alter table test_data alter column task_id type bigint using task_id::bigint

13. 刪除字段

1
alter table [表名] drop column [字段名];

14. 表中插入一行數據

1
insert into [表名] (字段 1 ,字段 2 ) values (值 1 ,值 2 );

例如:    命令行

1
insert into assist_info ( id , maat_id, block_type) values ( 'F006' 'F7775' , 1)  

  • 若是表中字段有大寫的字段,則須要對應的加上雙引號。例:insert into test (no, "Name") values ('123', 'jihite');
  • 值用單引號引發來(''),不能用雙引號("")

15. 表中刪除一行數據

1
delete from [表名] where [該行特徵];

16. 修改表中數據

1
update [表名] set [目標字段名]=[目標值] where [該行特徵]

17. 刪除表

1
drop table [表名];

18. 退出postgreSql

\q

19. 兩個查詢結果作差 except

1
2
3
4
5
(select node_id from node where node_id= 1  or node_id= 2 ) except (select node_id from node where node_id= 1 );
  node_id
---------
        2
( 1  row)

20. 複製表

CREATE TABLE test_a_copy AS SELECT * FROM test_a;

21.命令導入sql數據文件

psql -h localhost  -d databaseName  -U username -f  filename

22. 查詢結果存儲到輸出文件

格式:

\o file_path

這樣就會把查詢結果存儲到輸出文件中。例

postgres=> \o /home/jihite/data/iu_data;
postgres=> select test_id from cdb_all_iu_data limit 10;
postgres=> select test_id from cdb_all_iu_data limit 5;

結果

複製代碼
test_id
--------------
         2143
         2153
         2144
         2156
         2145
         2154
         2146
         2157
         2147
         2155
(10 rows)

test_id
--------------
         2143
         2153
         2144
         2156
         2145
(5 rows)
複製代碼

23. 數據庫的備份&恢復

導出到線下文件

pg_dump --host hostname --port port --username username -t tablename -d dbname >/home/jihite/table.sql 

把線下文件導入到數據庫

psql -h 10.125.7.68 -p 5432 -d postgres -U postgres -W postgres -f 2.sql

24. \x

複製代碼
postgres=> \x
Expanded display is on.
postgres=> select *  from cdb_chk_items where chk_id = 'R000000335';
-[ RECORD 1 ]+------------------------------------------------------------------------------------------------
chk_id       | R000000335
chk_desc     | 道路屬性與道路屬性相關檢查
chk_info     | {"FIELDS": {"TRAFFIC_SIGN": ["TYPE", "GEOM"], "ROAD_LINK": ["ROAD_CLASS", "FORM_WAY", "GEOM"]}}
err_desc     | {"ERR2": "roadclass取值錯誤", "ERR1": "formway取值錯誤"}
chk_level    | 1
is_opened    | 1
module_name  | TRAFFIC_SIGN
invalid_flag | 1
rel_mode     | MAIN_LAYER:TRAFFIC_SIGN
             :         TRAFFIC_SIGN|A,M|DIRECT
             :         ROAD_LINK|A,M,D|ATTR_REL
複製代碼

25. 從表A中把符合條件的記錄拷貝到表B

insert into A select * from B where id  in ('a', 'b', 'c');

26 創建索引

單字段索引

CREATE INDEX index_name ON table_name (field1);

多字段索引

CREATE INDEX index_name ON table_name (field1,field2);

查看全部表的索引使用狀況

複製代碼
select 
    relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch 
from 
    pg_stat_user_indexes 
order by 
    idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
複製代碼

查看某個表索引的使用狀況

複製代碼
select 
    relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch 
from 
    pg_stat_user_indexes 
where
    relname = table_name 
order by 
    idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
複製代碼

27. 超找數據庫的鏈接信息

select * from pg_stat_activity

包含:客戶端user、ip、執行語句,狀態、時間

相關文章
相關標籤/搜索