Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create New Database and a Schema in new database using Sql Script in PostgreSQL

I am trying to create a new database and a schema in the new database using Sql script as below. The database is 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;

Keep getting below error:

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

I have followed the answer below:
PostgreSQL: Create schema in specific database

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for your help.

>Solution :

Try:

\connect dezdaz.

You did not do CREATE DATABASE "DEZDAZ" so the database name is folded to lower case dezdaz per Identifiers. Looks like \connect is doing "DEZDAZ" and not finding the upper case version of the name. This is called out in 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.
         */
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading