MaxRev-Dev / gdal.netcore

GDAL 3.x C#/F# bindings for .NET apps
MIT License
161 stars 36 forks source link

Exception while trying to connect to PostgreSQL #124

Closed Sythelux closed 8 months ago

Sythelux commented 8 months ago

Describe the bug When doing Ogr.Open with a postgres connection string I get a exception saying connection to server at "localhost" (127.0.0.1), port 25432 failed: FATAL: no PostgreSQL user name specified in startup packet

To Reproduce I attached a minimal Program.cs connecting to a postgis database.

Expected behavior Based on my source code I would expect the Programm to open the database and print layer information.

Environment information:

Additional context

it is working, when I do ogrinfo -ro "postgresql://docker:docker@localhost:25432/gis" I actually suspect a libpq.so being packaged, that my OS doesn't use correctly.

The postgis is running in a docker and is exposed to localhost via port: 25432.

Programm.cs

using OSGeo.GDAL;
using OSGeo.OGR;

Ogr.RegisterAll();

DataSource? db = Ogr.Open("PG:dbname=gis host=localhost port=25432 user=docker password=docker", (int)Access.GA_ReadOnly);
// var db = Ogr.Open("postgresql://docker:docker@localhost:25432/gis", (int)Access.GA_ReadOnly); //<-- doesn't work either, when I uncomment
Console.WriteLine(db);
if (db != null)
{
    for (int i = 0; i < db.GetLayerCount(); i++)
    {
        var l = db.GetLayerByIndex(i);
        Console.WriteLine($"{l.GetName()}: {l.GetSpatialRef().GetName()}");
    }
}
else
{
    Console.WriteLine("DB not opened");
}
MaxRev-Dev commented 8 months ago
  1. You didn't read the README. You have to call GdalBase.ConfigureAll(); instead of Ogr.RegisterAll();. While executing your example I got an exception on missing "proj.db" which is configured by a call to ConfigureAll.
  2. Regarding your exception. It's not related to the bindings and clearly says there's no Postgress user name specified. By default, postgis has it's own user which configures the database. So if you get auth exception, you have to remove the specified username. Make sure your database is ready.

Here's the modified example.

docker run --name postgis-test -e POSTGRES_PASSWORD=user -e POSTGRES_DB=db -p 5432:5432 -d postgis/postgis

This part I ran in Ubuntu 22.04 with dotnet8 installed:

using MaxRev.Gdal.Core;
using OSGeo.GDAL;
using OSGeo.OGR;

GdalBase.ConfigureAll();

var db = Ogr.Open("PG:dbname=db host=host.docker.internal port=5432 user=postgres password=user", (int)Access.GA_ReadOnly);

Console.WriteLine(db);
if (db != null)
{
    for (int i = 0; i < db.GetLayerCount(); i++)
    {
        var l = db.GetLayerByIndex(i);
        Console.WriteLine($"{l.GetName()}: {l.GetSpatialRef().GetName()}");
    }
}
else
{
    Console.WriteLine("DB not opened");
}

Logs:

C# client

image

Database

image

Sythelux commented 8 months ago

no, I haven't read the README. I apologize, but I pretty much started using this package a year ago, because all other packages where a total mess with native bindings and did most stuff in try and error. I adjusted it now.

Thank you for verifying that the issue is not within the package itself. Changing the user to the default postgres user didn't help at all though, but I swapped the docker image I was using kartoza/postgis for some reason. I changed it to postgis/postgis now and it worked. This would've been the last thing to try if you haven't answered. So thank you.