Wanix
Wanix

Reputation: 21

npgsql 3.0: creating database through C# code

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?

Upvotes: 2

Views: 2685

Answers (1)

Shay Rojansky
Shay Rojansky

Reputation: 16672

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.

Upvotes: 1

Related Questions