Search code examples
c#databasenpgsqlcockroachdb

"A PostgreSQL type with the name 'json' was not found in the database" - Npgsql & cochroach db


Using this code, A PostgreSQL type with the name 'json' was not found in the database
Connection string

var connString = "Host=REMOVE;Port=26257;Username=daniel;Password=REMOVED;Database=REMOVED";
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
var dataSource = dataSourceBuilder.Build();
var conn = dataSource.OpenConnection();
await using (var cmd = new NpgsqlCommand("SELECT * FROM table_name", conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
    while (await reader.ReadAsync())
        Console.WriteLine(reader.GetString(0));
}

this result in console output of

A PostgreSQL type with the name 'json' was not found in the database
   at Npgsql.Internal.NpgsqlDatabaseInfo.GetPostgresTypeByName(String pgName)
   at Npgsql.TypeMapping.SystemTextJsonTypeHandlerResolver.PgType(String pgTypeName)
   at Npgsql.TypeMapping.SystemTextJsonTypeHandlerResolver..ctor(NpgsqlConnector connector, Dictionary`2 userClrTypes, JsonSerializerOptions serializerOptions)
   at Npgsql.TypeMapping.SystemTextJsonTypeHandlerResolverFactory.Create(TypeMapper typeMapper, NpgsqlConnector connector)
   at Npgsql.Internal.TypeMapping.TypeMapper.Initialize(NpgsqlDatabaseInfo databaseInfo, List`1 resolverFactories, Dictionary`2 userTypeMappings)
   at Npgsql.NpgsqlDataSource.Bootstrap(NpgsqlConnector connector, NpgsqlTimeout timeout, Boolean forceReload, Boolean async, CancellationToken cancellationToken)
   at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.PoolingDataSource.<Get>g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|42_0(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.NpgsqlDataSource.OpenConnection()
   at Program.<Main>$(String[] args) in C:\Users\danie\RiderProjects\ConsoleApp1\ConsoleApp1\Program.cs:line 7
   at Program.<Main>(String[] args)

Im using Jetbrains Rider for c# and it picks up the database just fine in the Database tool. I can insert and select using the console aswell, and read it using next.js code running on a Linode instance


Solution

  • I fixed it... It seems to be an issue with npgsql version 8.0.0-preview.4

    Switching to version 7.x will remove this error message.