使用 PostgreSQL 中的 Sql 脚本创建新数据库并在新数据库中创建架构

dos*_*ani 0 postgresql

我正在尝试使用 Sql 脚本创建一个新数据库并在新数据库中创建一个架构,如下所示。数据库是PostgreSql 14.1

    DROP DATABASE IF EXISTS DEZDAZ;
    
    CREATE DATABASE DEZDAZ
        WITH 
        OWNER = postgres
        ENCODING = 'UTF8'
        TABLESPACE = pg_default
        CONNECTION LIMIT = -1;
      
        \connect DEZDAZ;

        CREATE SCHEMA DEZDAZ;
Run Code Online (Sandbox Code Playgroud)

继续出现以下错误:

DROP DATABASE
CREATE DATABASE
error: \connect: connection to server at "localhost" (::1), port 5432 failed: FATAL:  database "DEZDAZ" does not exist
Run Code Online (Sandbox Code Playgroud)

我遵循了下面的答案: PostgreSQL: Create schema in特定数据库

感谢您的帮助。

Adr*_*ver 5

尝试:

\connect dezdaz

您没有这样做,数据库名称会根据IdentifiersCREATE DATABASE "DEZDAZ"折叠为小写。看起来正在做,但没有找到名称的大写版本。这被称为:dezdaz\connect"DEZDAZ"src/bin/psql/command.c

/*
 * Read and interpret an argument to the \connect slash command.
...

/*
         * Ideally we should treat the arguments as SQL identifiers.  But for
         * backwards compatibility with 7.2 and older pg_dump files, we have to
         * take unquoted arguments verbatim (don't downcase them). For now,
         * double-quoted arguments may be stripped of double quotes (as if SQL
         * identifiers).  By 7.4 or so, pg_dump files can be expected to
         * double-quote all mixed-case \connect arguments, and then we can get rid
         * of OT_SQLIDHACK.
         */
Run Code Online (Sandbox Code Playgroud)