Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using npgsql with CockroachDB and .Net Core

I successfully used npgsql (Npgsql.EntityFrameworkCore.PostgreSQL) to connect to PostgreSQL database. I have heard that same EF provider works well with CockroachDB. I am trying with it but need help to define connection string for CockroachDB. For PostgreSQL I have used following connection string:

optionsBuilder.UseNpgsql(@"Host=localhost;Database=<database>;Username=postgres;Password=<password>");

Does anybody know what should a connection string look like if I wish to connect it to CockroachDB?

like image 629
jn_sudeep Avatar asked May 03 '26 19:05

jn_sudeep


1 Answers

The connection string you mentioned is almost correct, you just need to add Port=26257 as CockroachDB runs on a dedicated port.

If the Cockroach nodes are running securely, you'll also need to specify the Security and Encryption parameters.

You can find sample code to use npgsql to talk to CockroachDB in the docs, including the important "retry logic" section.

like image 145
Marc Avatar answered May 07 '26 11:05

Marc