我正在尝试自动化一组创建模板数据库的过程。
我有一组文件(file1、file2、...fileN),每个文件都包含创建模板数据库所需的一组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
我希望能够编写一个shell脚本来执行文件中的命令,这样我就可以编写这样的脚本:
# 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
对如何做到这一点有什么建议吗?(您可能已经猜到了,我是一个shell脚本新手。)
编辑
为了进一步澄清这个问题,我想知道:
https://stackoverflow.com/questions/8594717
复制相似问题