PostgreSQL - 自动化模式和表创建 - powershell

时间:2014-01-15 19:53:44

标签: sql postgresql powershell automation

我正在尝试自动将模式和一些表创建到新创建的模式中。我试图在powershell中编写一个脚本来帮助我实现同样的目标。我已经能够创建模式,但是,我无法将表创建到该模式中。

我正在将新模式作为变量创建到powershell。

到目前为止

脚本(基于以下答案的解决方案。StackOverFlow Solution):


$MySchema=$args[0]

$CreateSchema = 'CREATE SCHEMA \"'+$MySchema+'\"; set schema '''+$MySchema+''';'
write-host $CreateSchema
C:\PostgreSQL\9.3\bin\psql.exe -h $DBSERVER -U $DBUSER -d $DBName -w -c $CreateSchema

# To create tables
C:\PostgreSQL\9.3\bin\psql.exe -h $DBSERVER -U $DBUSER -d $DBName -w -f 'E:\automation\scripts\create-tables.sql' -v schema=$MySchema

执行时,我看到以下错误:

psql:E:/automation/scripts/create-tables.sql:11: ERROR:  no schema has been selected to create in

create-tables.sql的内容是:

SET search_path TO :schema;

CREATE TABLE testing (
  id SERIAL,
  QueryDate varchar(255) NULL
);

1 个答案:

答案 0 :(得分:1)

你的第一步就是这个:

$CreateSchema = 'CREATE SCHEMA \"'+$MySchema+'\"; set schema '''+$MySchema+''';'

取出set schema - 它是错误的并导致不创建架构。然后在下一步中,您将找到一个空的搜索路径(因为架构永远不会被创建),这就是您收到该错误的原因。

相关问题