mateuszlewko
mateuszlewko

Reputation: 1129

Npgsql throws an error: wrong connection string. Can't connect to postgreSQL [F#]

I'm using SQLProvider to connect to my (local) PostgreSQL database in F#. I've started with this code from fsprojects:

open FSharp.Data.Sql
open Npgsql

let [<Literal>] ResolutionPath = 
    __SOURCE_DIRECTORY__ + @"/../../packages/Npgsql/lib/net451/"

[<Literal>]
let connectionString = "Host=localhost;Port=5432;User ID=test;Password=test;Database=testdb;"

 type PostgreSQL = 
    SqlDataProvider< 
        Common.DatabaseProviderTypes.POSTGRESQL,
        ConnectionString  = connectionString,
        ResolutionPath    = ResolutionPath,
        IndividualsAmount = 1000,
        UseOptionTypes    = true>

When I'm trying to compile it I get this error message:

(path)/Database.fs(60,9): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: The type initializer for 'Npgsql.Counters' threw an exception. (path)/Database.fs(60,9): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not create the connection, most likely this means that the connectionString is wrong. See error from Npgsql to troubleshoot: The type initializer for 'Npgsql.Counters' threw an exception.

I've checked that user id and password and everything in connection string is correct. I actually don't have any idea how to debug this issue. Do you know what could be wrong? Does it work for you without any issues?

I'm using macOS 10.12.

And lastly, in case I won't be able to fix this, are there any other methods that you would recommend for connecting to postgreSQL (with type providers)?

Upvotes: 2

Views: 1756

Answers (1)

Shay Rojansky
Shay Rojansky

Reputation: 16692

The error message clearly says that an exception was thrown from the type initializer for Npgsql.Counters - this is Npgsql 3.2's new support for Windows performance counters.

Unfortunately it seems that there are several issues with performance counters in various scenario (see #1447 and #1435). Because of this, version 3.2.2 (to be released this week) won't make use of them by default.

Can you please confirm that you're using 3.2.1 (and not 3.2.0), because a partial fix was introduced? If you're using 3.2.1 and getting this exception, you'll have to wait a few days for 3.2.0, and can use 3.1.10 in the meantime where the exception won't be thrown.

Upvotes: 4

Related Questions