如何使用bash在postgres中运行alter table脚本

时间:2016-09-07 07:37:47

标签: sql bash postgresql ddl

我想使用bash脚本运行alter table命令。我设法创建表,加载basemodel,创建配置表等。脚本将在执行alter table命令之前登录到postgres数据库。它停留在(abcdb =>)而不继续执行alter table命令。有没有办法确保alter table能够执行?

登录为

psql -h 191.169.51.10 -d abcdb -U myname



 alter table attr_config rename regexp to regexp_val;
 alter table class_action_config rename type to type_name;
 alter table funcitem_config rename type to type_name;

1 个答案:

答案 0 :(得分:1)

为了运行这样的脚本,您需要将SQL / DML(alter table语句)重定向到psql命令中。否则bash不会理解如何处理它们。

psql -h 191.169.51.10 -d abcdb -U myname << EOF

 alter table attr_config rename regexp to regexp_val;
 alter table class_action_config rename type to type_name;
 alter table funcitem_config rename type to type_name;
EOF

或者,您可以将SQL / DML放入单独的文件中,并使用psql从中读取:

psql -h 191.169.51.10 -d abcdb -U myname < alter_statements.sql

psql -h 191.169.51.10 -d abcdb -U myname -f alter_statements.sql