1

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 ?

struggling
  • 535
  • 1
  • 10
  • 25
  • Add `database=postgres` in connection string and try – Vivek S. Dec 09 '15 at 10:08
  • You provided the `Database` parameter in your second `connStr` example, you leave it out in the first. Perhaps this is a factor in why it succeeded? As a side note, never store credentials in code like this – DGibbs Dec 09 '15 at 10:08
  • @DGibbs the credential are wrong :P , Here i have given just for simulation – struggling Dec 09 '15 at 10:14
  • @struggling It doesn't matter that they are invalid, I'm telling you in general to never store them in code. – DGibbs Dec 09 '15 at 10:15

1 Answers1

5

You need to add Database=postgres in the connection string

 connStr = "Server=localhost;Port=5432;User Id=postgres;Password=password_of_the_user;Database=postgres";

Because postgres is the default administrative connection database of PostgreSQL

Vivek S.
  • 19,945
  • 7
  • 68
  • 85
  • 1
    @struggling That message is very clear .... *Password authentication is failed for user openpg* and this is not related with my answer . Just check my update – Vivek S. Dec 09 '15 at 10:14
  • I write teh same password while opening my PGAdmin here in red box http://prntscr.com/9c2oc1 and it opens, and same password i use when created manually the table for connection string, but here it gives this error, i dont knwo why – struggling Dec 09 '15 at 10:22
  • I guess you're missing something somewhere meanwhile just use this http://hastebin.com/tonafatake.vbs (_add your password in it_) – Vivek S. Dec 09 '15 at 10:25
  • ok, i got it, i changed postgres with openpg it worked, but how to check if datbase already exist ? Because it is giving syntax error at or near NOT in line "CREATE DATABASE IF NOT EXISTS testDb ;" – struggling Dec 09 '15 at 10:29
  • `CREATE DATABASE IF NOT EXISTS testDb` is invalid query in PostgreSQL OTOH you'll get some workarounds form here -http://stackoverflow.com/questions/18389124/simulate-create-database-if-not-exists-for-postgresql – Vivek S. Dec 09 '15 at 10:40
  • i am beginner of Database, please explain me in detail, that would be great if you write an answer, upon understanding i can also mark you as answer this way. – struggling Dec 09 '15 at 10:47
  • This answer is already given solution for the problem that you've mentioned in your question – Vivek S. Dec 09 '15 at 10:50
  • @struggling try this - http://hastebin.com/lokezafepe.dos (_give password for the user `openpg`_ in `password=password_of_the_user`) – Vivek S. Dec 09 '15 at 10:57
  • Connection error, may be because of no port, Server etc in the given code, and do ihave to write dbname=postgres or Database=posgres ? – struggling Dec 09 '15 at 11:25
  • i have marked you as answer, but please try to answer the part of my question – struggling Dec 09 '15 at 12:26