简体   繁体   English

如何使用bash在postgres中运行Alter Table脚本

[英]How to run alter table script in postgres using bash

I wanted to run the alter table command using bash script. 我想使用bash脚本运行alter table命令。 I managed to create the table, load the basemodel, create config tables and etc. The script will login to the postgres database before it is execute the alter table command. 我设法创建了表,加载了基本模型,创建了配置表等。脚本将在执行alter table命令之前登录到postgres数据库。 It stuck as (abcdb=> ) without proceed to the alter table command. 它卡住了(a​​bcdb =>),而没有继续执行alter table命令。 Is there any way to make sure the alter table able to execute? 有什么方法可以确保alter table能够执行?

The login as 登录为

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;

In order to run a script like this you need to redirect the SQL/DML (alter table statements) into the psql command. 为了运行这样的脚本,您需要将SQL / DML(更改表语句)重定向到psql命令中。 Otherwise bash won't understand what to do with them. 否则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

Alternatively you can put your SQL/DML into a separate file and have psql to read from that: 或者,您可以将SQL / DML放入一个单独的文件中,并使用psql读取该文件:

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

Or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM