您的位置:首页 > 数据库

postgreSql 入门常用命令

2015-07-22 15:51 543 查看

1. 命令行登录数据库

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


2. 列出所有数据库

\l


3. 切换数据库

\c dbname


4. 列出当前数据库的所有表

\d


5. 查看指定表的所有字段

\d tablename




6. 查看指定表的基本情况

\d+  tablename




7. 退出操作

q


8. 新建表

create table REL_CROSS_NODE(ID integer, CROSS_ID integer);


9. 删除表

drop table REL_CROSS_NODE;


10. 清空表

delete from [表名]


11. 添加字段

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


12. 更改字段

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

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


13. 删除字段

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


14. 表中加入一行数据

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


例如:

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


:id,maat_id,block_type字段名字不能加引号 

15. 表中删除一行数据

delete from [表名] where [该行特征];


16. 修改表中数据

update [表名] set [目标字段名]=[目标值] where [该行特征]


17. 删除表

drop table [表名];


18. 退出postgreSql

\q


19. 两个查询结果做差 except

(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
http://blog.chinaunix.net/uid-20802110-id-4825981.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: