database 使用带有 [template]、[encoding]、[owner] 和 .sql 文件的批处理文件创建 Postgres 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8208181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:23:07  来源:igfitidea点击:

Create Postgres database using batch file with [template],[encoding],[owner] and a .sql file

databasepostgresqlbatch-filedatabase-connectionddl

提问by PresleyDias

I want to create a Postgres database using a batch file. Now the normal way of doing this is the following:

我想使用批处理文件创建 Postgres 数据库。现在执行此操作的正常方法如下:

"C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin MydatAbseName

"C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin MydatAbseName

This script above creates a database with the default database parameters. However, I want to create a database with the following parameters, as follows:

上面的这个脚本使用默认的数据库参数创建了一个数据库。但是,我想使用以下参数创建一个数据库,如下所示:

   WITH OWNER = Myadmin 
   TEMPLATE = template0 
   ENCODING = 'SQL_ASCII'
   TABLESPACE = pg_default
   LC_COLLATE = 'C'
   LC_CTYPE = 'C'
   CONNECTION LIMIT = -1;

Please tell me how to create a database with the above parameters using Batch files.

请告诉我如何使用批处理文件创建具有上述参数的数据库。

Also let me know how to use a .sql file to do the same, like this command-line:

也让我知道如何使用 .sql 文件来做同样的事情,就像这个命令行:

"C:\Program Files\PostgreSQL.0\bin\createdb.exe" -U Myadmin -f C:\createDB.sql;

回答by Erwin Brandstetter

The client program createdbdoes not support all those options.
Create a file db_create.sql:

客户端程序createdb不支持所有这些选项。
创建一个文件db_create.sql

CREATE DATABASE MydatAbseName
   WITH OWNER myadmin 
   TEMPLATE template0
   ENCODING 'SQL_ASCII'
   TABLESPACE  pg_default
   LC_COLLATE  'C'
   LC_CTYPE  'C'
   CONNECTION LIMIT  -1;

Call it:

称它为:

psql -U postgres postgres -f C:/path/to/db_create.sql

The trick here is to connect to the default maintenance db "postgres"and create the new database from there. I do it with the default superusernamed "postgres" in my example.
psql -fexecutes the SQL commands in the given file.

这里的技巧是连接到默认维护数据库“postgres”并从那里创建新数据库。在我的示例中,我使用名为“postgres”的默认超级用户执行此操作。
psql -f执行给定文件中的 SQL 命令。

You could also just execute a single command with psql -c(no file involved):

您也可以只执行一个命令psql -c(不涉及文件):

psql -U postgres postgres -c "CREATE DATABASE MydatAbseName WITH OWNER Myadmin
EMPLATE template ENCODING 'SQL_ASCII' TABLESPACE  pg_default LC_COLLATE  'C'
LC_CTYPE  C' CONNECTION LIMIT  -1"

More on creating a database in the fine manual hereand here.
More on psql.

更多关于在此处此处的精美手册中创建数据库的信息
更多关于psql



On Windows, it looks something like this:

在 Windows 上,它看起来像这样:

"C:\Program Files\PostgreSQL\verson_number\bin\psql.exe" -U user -f C:/path/to/db_create.sql postgres

The last "postgres" is the name of the default maintenance db. If you want to use it in a batch file you have to answer a password prompt or connect with a user that is allowed access without providing a password. Basics in chapters The Password Fileand The pg_hba.conf Fileof the manual. More here:

最后一个“postgres”是默认维护数据库的名称。如果您想在批处理文件中使用它,您必须回答密码提示或与无需提供密码即可访问的用户连接。手册的密码文件pg_hba.conf 文件章节中基础知识。更多在这里: