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

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

问题描述

我正在尝试使创建TEMPLATE数据库的一组过程自动化。

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

我有一组文件(file1,file2,... fileN),每个文件其中包含创建TEMPLATE数据库所需的一组pgsql命令。

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

文件(createdbtemplate1.sql)的内容大致如下:

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

我希望能够编写一个将执行文件中命令的Shell脚本,以便可以编写如下脚本:

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

关于如何执行此操作的任何建议? (您可能已经猜到了,我是一个shell脚本新手。)

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

再问一个问题,我想知道:

To clarify the question further, I want to know:


  1. 如何编写groksqlcommands.sh(一个bash脚本,它将从文件中运行一组pgsql cmds )

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


推荐答案

首先,不要混合 psql 元命令和 SQL 命令。这些是单独的命令集。有一些技巧可以结合使用这些技巧(使用psql元命令 \o \\ 和管道字符串)

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.


  • 使文件仅包含SQL命令。

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

  • 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假设您以系统用户 postgres 的身份运行,并且具有 postgres 作为Postgres超级用户。由于 IDENT postgres 都具有无密码访问权限>在 pg_hba.conf 中进行设置(默认设置)。

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"

我将系统模板db template0 上的新模板db。在此处阅读更多相关信息

您的问题:

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


如何(...)从文件运行一组pgsql cmds

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

尝试:

psql mytemplate1 -f file

批处理示例:

#! /bin/sh

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

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

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'

命令选项 -c 使 psql 执行单个SQL命令字符串。可以是多个命令,以终止; -将在一个事务中执行,并且仅返回最后一条命令的结果。

阅读手册中的 psql命令选项

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.

如果不提供要连接的数据库,则 psql 将使用名为 postgres的默认维护数据库。在第二个答案中,与我们连接到哪个数据库无关。

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.

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

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