1

Please i want to creat a Database if not exist, and after if the Database exist i want to creat tables.

I use this code :

using Npgsql;

 string server = "localhost";
 string database = "MyNewDatabase";
 string password = "password1234";
 string connectionString = @"Server=" + server + ";User Id=postgres; Password=" + password + ";";
 var connection = new NpgsqlConnection(connectionString);
 string commandText = string.Format("CREATE DATABASE IF NOT EXISTS \"{0}\";", database);
 var command = new NpgsqlCommand(commandText);

 connection.Open();
 command.Connection = connection;
 command.ExecuteNonQuery();
 connection.Close();

the problem is i get a error : 42601 (syntax error using « NOT »).

Using C#, i am trying to verify if the database exist, if not we will creat it, and after we will verify if the tables exist, if they not exist we will creat them.

Please, may someone tell me how to do it?

Thanks stackoverflowers

Joe
  • 447
  • 12
  • 25
  • 2
    What made you think you could use `IF NOT EXISTS` there? [The manual sure doesn't mention it](http://www.postgresql.org/docs/current/static/sql-createdatabase.html). You'll need to query `pg_database` to see if it exists, and create it if it doesn't. There's a race condition there that you can't easily fix. – Craig Ringer Aug 02 '15 at 22:49
  • This question has been posted (and answered) before [here.](http://stackoverflow.com/questions/17838913/postgresql-create-database-table-dynamically) – gyslinn Jan 31 '17 at 21:20

0 Answers0