Lee
Lee

Reputation: 17

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;

Upvotes: 1

Views: 2658

Answers (1)

Philip Couling
Philip Couling

Reputation: 14883

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

Upvotes: 2

Related Questions