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

How to write PL/pgsql DDL to create schemas in redshift and then looping the ddls to create tables in the respective schemas?

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

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 "" -t -c "$sql"`;
  # If $check is `null`
  if [ -z "$check" ]; then 
      sql="CREATE SCHEMA $p;";
      create=`psql -d "" -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 "    --------------------------------------------"