Make running lots of multi-line sql statements prettier
#!/bin/bash set -e set -u RUN_ON_MYDB="psql -X -U myuser -h myhost --set ON_ERROR_STOP=on --set AUTOCOMMIT=off mydb" $RUN_ON_MYDB <<SQL drop schema if exists new_my_schema; create table my_new_schema.my_new_table (like my_schema.my_table); create table my_new_schema.my_new_table2 (like my_schema.my_table2); commit; SQL $RUN_ON_MYDB <<SQL create index my_new_table_id_idx on my_new_schema.my_new_table(id); create index my_new_table2_id_idx on my_new_schema.my_new_table2(id); commit; SQL
Note too that you can use fun bash tricks to assign to multiline variables and feed those to psql later:
CREATE_MY_TABLE_SQL=$(cat <<EOF
create table foo (
id bigint not null,
name text not null);
EOF
)
$RUN_ON_MYDB <<SQL
$CREATE_MY_TABLE_SQL
commit;
SQL