如何使用 bash 运行 改变 postgres 中的 table 脚本

How to run alter table script in postgres using bash

我想 运行 使用 bash 脚本更改 table 命令。我设法创建了 table、加载基本模型、创建配置 tables 等等。脚本将在执行 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;

为了 运行 这样的脚本,您需要将 SQL/DML (更改 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