简体   繁体   中英

How can I call a postgresql db stored in DigitalOcean from ASP.NET Core web api?

I'm tying to use the connection string provided by DigitalOcean directly on the appsettings.json file, but shows error

ArgumentException: Keyword not supported: postgresql://XXXXX_user:xxxxxxxxxxxxx@db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/trackbus?sslmode (Parameter 'keyword') Npgsql.NpgsqlConnectionStringBuilder.GetProperty(string keyword)

This is the exact connection string provided by DigitalOcean:

postgresql://xxxx_user:xxxxx@db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/TrackBus?sslmode=require

I'm using Npgsql nuget package, when the db was local was working fine using th regular connection string

"Server=xxx.0.0.x;Port=xxxx;Database=TrackBus;User Id=postgres;Password=xxxx;"

I've also tried this (private and public):

"Server=private-db-trackbus-nyc3-xxxxx-do-user-xxxxx-0.b.db.ondigitalocean.com;Port=xxxxx;Database=TrackBus;User Id=xxxxx_user;Password=xxxxxxxxxx;Integrated Security=true;"

Error:

An unhandled exception occurred while processing the request. PostgresException: 28000: no pg_hba.conf entry for host "xxx.xxx.xxx.xxx", user "xxxx", database "TrackBus", SSL off

I searched this last 28000 exception but couldn't apply any solution found.

The connection string provided by DigitalOcean is for CLI:

postgresql://xxxx_user:xxxxx@db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/TrackBus?sslmode=require

You have to convert it to the format that appsettings.json uses and it will be like this:

"Server=db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com;Port=xxxxx;Database=TrackBus;User Id=xxxxxxxx;Password=xxxxxxxxx;sslmode=Require;Trust Server Certificate=true;"

The sslmode=Require;Trust Server Certificate=true; is super important because the managed database in DigitalOcean has it that that one.

Also remember that all postgresql database by default are blocking remote connection, you have to add the permission on the pg_hba.conf file from the Postgresql installation folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM