I am trying to create a postGres database c# programatically, previously i had created DB manually and teh conection string to open it and do operations on it and it was working nicely.
Now i have to first check if that DBB already exist if not then create it first and then do other operations liek reading writting to it.
My code to do so is :
internal void createDataBaseIfDoNotExist()
{
string connStr = string.Empty;
connStr =
"Server=" + "localhost"
+ ";Port=" + "5432"
+ ";User Id=" + "openpg"
+ ";Password=" + "oppwd"
+ ";";
var m_conn = new NpgsqlConnection(connStr);
var m_createdb_cmd = new NpgsqlCommand(@"CREATE DATABASE IF NOT EXISTS testDb ;", m_conn);
try
{
m_conn.Open();
m_createdb_cmd.ExecuteNonQuery();
m_conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Errr on createDataBaseIfDoNotExist query :" + ex);
}
}
And it gives followign error:
database "openpg" does not exist Severity: FATAL Code: 3D000
Whereas the database name i have kept "testDb", it's not "openpg"
Previously i created a DB name "NewDB" and created connection string like this :
connStr =
"Database=" + "NewDB"
+ ";Server=" + "localhost"
+ ";Port=" + "5432"
+ ";User Id=" + "openpg"
+ ";Password=" + "oppwd"
+ ";";
And it was workign fine, So why have error now and how to cure it ?