TEMPLATEデータベースを作成する一連の手順を自動化しようとしています。
一連のファイル(file1、file2、... fileN)があり、各ファイルには、TEMPLATEデータベースの作成に必要な一連のpgsqlコマンドが含まれています。
ファイル(createdbtemplate1.sql)の内容は、おおよそ次のようになります。
CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8';
\c mytemplate1
CREATE TABLE first_table (
--- fields here ..
);
-- Add C language extension + functions
\i db_funcs.sql
ファイル内のコマンドを実行するシェルスクリプトを記述できるようにしたいので、次のようなスクリプトを記述できます。
# run commands to create TEMPLATE db mytemplate1
# ./groksqlcommands.sh createdbtemplate1.sql
for dbname in foo foofoo foobar barbar
do
# Need to simply create a database based on an existing template in this script
psql CREATE DATABASE $dbname TEMPLATE mytemplate1
done
これを行う方法について何か提案はありますか?(ご想像のとおり、私はシェルスクリプトの初心者です。)
編集
質問をさらに明確にするために、私は知りたいです:
- groksqlcommands.sh(ファイルから一連のpgsqlコマンドを実行するbashスクリプト)の記述方法
- コマンドラインで既存のテンプレートに基づいてデータベースを作成する方法