2

Earlier, in npgsql 2.2 I could create a new PostgreSQL database through c# code like this:

string strconn = String.Format("Server={0};Port={1};User Id={2};Password={3};",
  strServer, strPort, strUser, strPass);

NpgsqlConnection admconn = new NpgsqlConnection(strconn);

admconn.Open();
NpgsqlCommand command = new NpgsqlCommand("CREATE DATABASE " + strNewDB, admconn);
command.ExecuteNonQuery();
admconn.Close();

So, you see, there's no Database name in connection string. But when I was migrated to npgsql 3.0 this is not work: on opening the connection the exception occured: "Database can't be null".

If I specify in connection string default database "postgres" it works, but the joke is that it can be no default postgres database or even databases at all in PostgreSQL and it would be better not specify database to create new one.

And the same situation in creating user through c# code.

Any ideas?

1
  • 1
    You have to connect to some database. template1 is always there, so you can connect to that, and then run the create database statement. Commented Aug 17, 2015 at 16:53

1 Answer 1

1

This is an issue in 3.0.0 and has already been fixed - 3.0.1 will be released with this fix within a few days.

https://github.com/npgsql/npgsql/issues/703

Just FYI, as @a_horse_with_no_name said, in PostgreSQL you always connect a database. If you omit the database name PostgreSQL simply defaults to a database matching the user name.

Sign up to request clarification or add additional context in comments.

Comments

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.