简体   繁体   中英

How to run alter table script in postgres using bash

I wanted to run the alter table command using bash script. 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. It stuck as (abcdb=> ) without proceed to the alter table command. Is there any way to make sure the alter table able to execute?

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. Otherwise bash won't understand what to do with them.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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