如何编写PL / pgsql DDL以在redshift中创建模式,然后循环ddls以在相应的模式中创建表?

时间:2016-11-22 20:23:00

标签: postgresql plpgsql amazon-redshift ddl

我正在尝试使用将在redshift中执行的ddl创建多个模式,然后围绕这些模式循环执行我已创建的ddls,以便在这些模式中创建表。任何人都可以建议我做正确的方法吗?

1 个答案:

答案 0 :(得分:0)

Redshift不支持PL/pgsql。您需要在外部执行此操作,例如,使用shell脚本。

#!/bin/bash

# Create schemas in schema list
# * Generate check SQL
# * Check if schema exists
# * Create schema if it doesn't

echo "    --------------------------------------------"
echo "    CREATING SCHEMAS >>"
while read p; do
  echo "    '$p' >>";
  sql="SELECT 1 as exists FROM information_schema.schemata WHERE schema_name = '$p';"
  check=`psql -d "$1" -t -c "$sql"`;
  # If $check is `null`
  if [ -z "$check" ]; then 
      sql="CREATE SCHEMA $p;";
      create=`psql -d "$1" -c "$sql";`
      # If $create = `CREATE SCHEMA`
      if [ "$create" = "CREATE SCHEMA" ]; then
          echo "        << '$p' - CREATED";
      else
          echo "        << '$p' - ERROR";
      fi
  else 
      echo "        << '$p' - EXISTS";
  fi
done < ../Configuration/SETUP_Schemas.txt
echo "    << DONE"
echo "    --------------------------------------------"