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

2
  • 2
    What made you think you could use IF NOT EXISTS there? The manual sure doesn't mention it. 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. Commented Aug 2, 2015 at 22:49
  • This question has been posted (and answered) before here. Commented Jan 31, 2017 at 21:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.