Skip to content

create/dropDatabase failed on postgresql #10030

Closed

Description

@ALiangLiang

What are you doing?

In this post says, postgresql not support CREATE DATABASE IF NOT EXISTS.... But in #10027, I use if not exists as MySQL.

I'm trying to fix this issue.

What do you expect to happen?

Run createDatabase correctly on postgresql.

What is actually happening?

Failed with ERROR: syntax error at or near "NOT"

Dialect: postgres
Dialect version: XXX
Database version: sushantdhiman/postgres:9.5
Sequelize version: a7fa316
Tested with latest release: Yes, a7fa316

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. ALiangLiang commented on Oct 15, 2018

    @ALiangLiang
    ContributorAuthor

    Note: CREATE DATABASE cannot executed within multi-command string.

    Two ways:

    1. Abandon checking the db exists
    2. Take out the exists checking to QueryInterface. It's take two queries in one time API invoking.
      createDatabase(database, options) {
        options = options || {};
        const sql = this.QueryGenerator.createDatabaseQuery(database, options);
        if (IS_POSTGRES) {
          return this.sequelize.query(`SELECT 1 as exists FROM pg_database WHERE datname = '${database}'`)
            .then(result => {
              if (result[0].exists !== 1) {
                return this.sequelize.query(sql, options);
              }
            });
        } else {
          return this.sequelize.query(sql, options);
        }
      }

    I prefer method 2.

  2. sushantdhiman commented on Oct 15, 2018

    @sushantdhiman
    Contributor

    Yeah, go with simplest for now

    1. Abandon checking the db exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions