Postgres 常用操作手札

导出表数据到sql文件

pg_dump -h hostname -p 5432 -U username -d database -t tablename > /tmp/file_name.sql

修改列的类型

�alter table applydata_xq alter COLUMN status type bit varying(16);
直接修改为bit varying(num)会抛出

ERROR:  column "status" cannot be cast automatically to type bit varying  
HINT:  You might need to specify "USING status::bit varying(16)".  

删除字段后再添加status字段

alter table applydata_xq drop status;
alter table applydata_xq add status bit varying(16) not null default B'0'::bit(16); 

从文本中直接导入psql终端执行批量执行
cat sql.txt | ~/pg_client/psql -h <host> "dbname=<db_name> user=<username> password=<password>"

postgresql—-根据现有表创建新表
create table table_name (like parent_table {INCLUDING|EXCLUDING}{DEFAULTS|CONSTRAINTS|INDEXES|STORAGE|COMMENTS|ALL});

postgres中字符串数据的插入请使用单引号’

    原文作者:一条湫刀鱼
    原文地址: https://www.jianshu.com/p/5bb01f51bbc9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞