的bash shell脚本来执行文件中的pgsql命令 [英] bash shell script to execute pgsql commands in files

查看:299
本文介绍了的bash shell脚本来执行文件中的pgsql命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自动化一套创建模板的数据库程序。

我有一组文件(文件1,文件2,... fileN),其中每个都包含了一组用于创建模板数据库所需的pgsql命令。

文件(createdbtemplate1.sql)的内容看起来大致是这样的:

  CREATE与编码UTF8'DATABASE mytemplate1;\\ C mytemplate1CREATE TABLE first_table(
 ---在这里领域..
); - 将C语言的扩展功能+
\\我db_funcs.sql

我希望能够写一个shell脚本,将在文件中执行的命令,让我可以写这样的脚本:

 #运行命令来创建模板数据库mytemplate1
#./groksqlcommands.sh createdbtemplate1.sql在富foofoo foobar的BARBAR DBNAME

    #需要简单地创建基于这个脚本现有模板数据库
    PSQL CREATE DATABASE $ DBNAME模板mytemplate1
DONE

这是如何做到这一点有什么建议? (正如你可能已经猜到了,我是一个shell脚本新手。)

修改

要进一步明确的问题,我想知道:


  1. 如何写groksqlcommands.sh(将运行从文件一组的pgsql CMDS的bash脚本)

  2. 如何创建一个基于现有的模板在命令行中的数据库


解决方案

首先,做的不可以混合的psql 元命令和 SQL 命令。这些命令单独设置。有技巧结合的(使用psql元命令 \\ 0 \\\\ 和管道的字符串给psql在壳),但保持距离,如果你能。


  • 请您的文件只包含SQL命令。

  • 请不要在SQL文件中的CREATE DATABASE语句。单独创建的数据库,你的多个的要在同一模板数据库执行的文件。

我假设你是作为系统用户的Postgres 运行,你有的Postgres 为Postgres超级用户。默认端口5432和用户的Postgres 在同一个数据库集群中的所有数据库有由于一个无密码访问识别在设置的pg_hba.conf (默认设置)。

  psql的Postgres的-c创建与编码UTF8DATABASE mytemplate1
                  模板template0而

我基地新模板数据库的系统模板分贝 template0中。阅读更多关于这里。结果
你的问题:


  

如何(...)从文件中运行一组的pgsql CMDS的


尝试:

  PSQL mytemplate1 -f文件

例如批处理:

#! / bin / sh的在/路径文件/到/文件/ *;做
    PSQL mytemplate1 -f$文件
DONE

该命令选项 -f ,使的psql 在文件中执行SQL命令。


  

如何创建一个基于现有的模板在命令行中的数据库


 的psql -cCREATE DATABASE MYDB模板mytemplate1

该命令选项 -c ,使的psql 执行一个SQL命令字符串。可以是多个命令,由终止; - 将在有一个的事务,最后一个命令的唯一结果返回执行结果。
阅读关于在手动 psql的命令选项。

如果你不提供一个数据库连接,的psql 将使用名为默认维护数据库的Postgres 。在第二个答案是不相关的,我们连接到的数据库。

I am trying to automate a set of procedures that create TEMPLATE databases.

I have a set of files (file1, file2, ... fileN), each of which contains a set of pgsql commands required for creating a TEMPLATE database.

The contents of the file (createdbtemplate1.sql) looks roughly like this:

CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8';

\c mytemplate1

CREATE TABLE first_table ( 
 --- fields here ..
);

-- Add C language extension + functions
\i db_funcs.sql

I want to be able to write a shell script that will execute the commands in the file, so that I can write a script like this:

# 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

Any suggestions on how to do this? (As you may have guessed, I'm a shell scripting newbie.)

Edit

To clarify the question further, I want to know:

  1. How to write groksqlcommands.sh (a bash script that will run a set of pgsql cmds from file)
  2. How to create a database based on an existing template at the command line

解决方案

First off, do not mix psql meta-commands and SQL commands. These are separate sets of commands. There are tricks to combine those (using the psql meta-commands \o and \\ and piping strings to psql in the shell), but stay away from that if you can.

  • Make your files contain only SQL commands.
  • Do not include the CREATE DATABASE statement in the SQL files. Create the db separately, you have multiple files you want to execute in the same template db.

I assume you are operating as system user postgres and you have postgres as Postgres superuser. All databases in the same db cluster on the default port 5432 and the user postgres has password-less access due to an IDENT setting in pg_hba.conf (default settings).

psql postgres -c "CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8'
                  TEMPLATE template0"

I base the new template db on the system template db template0. Read more about that here.
Your question:

How to (...) run a set of pgsql cmds from file

Try:

psql mytemplate1 -f file

Example for batch:

#! /bin/sh

for file in /path/to/files/*; do
    psql mytemplate1 -f "$file"
done

The command option -f makes psql execute SQL commands in a file.

How to create a database based on an existing template at the command line

psql -c 'CREATE DATABASE myDB TEMPLATE mytemplate1'

The command option -c makes psql execute a single SQL command string. Can be multiple commands, terminated by ; - will be executed in one transaction and only the result of the last command returned.
Read about psql command options in the manual.

If you don't provide a database to connect to, psql will use the default maintenance database named postgres. In the second answer it is irrelevant which database we connect to.

这篇关于的bash shell脚本来执行文件中的pgsql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆