npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.52k stars 223 forks source link

.Net Core, docker, ubuntu, conection issue #166

Closed galakt closed 7 years ago

galakt commented 7 years ago

1) Installed VS2017, created asp.net core web api sln, added docker support. 2) Generated classes and context with "dotnet ef dbcontext scaffold". 3) Run under docker and tried to retrieve data from postgres. Got exception [1]. Meanwhile all works fine when run under IIS.

Is it my bad or it is real issue?

Looks like dup https://github.com/npgsql/npgsql/issues/874 https://github.com/npgsql/npgsql/issues/1061 https://github.com/npgsql/npgsql/issues/905

[1]
An exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code.

$exception  {System.Net.Sockets.SocketException: Connection refused
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout)
   at Npgsql.ConnectorPool.Allocate(NpgsqlConnection conn, NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnection.OpenInternal()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at *controller* in *path to controller*
   at Microsoft.AspNetCore.Mvc.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()}  System.Net.Sockets.SocketException
roji commented 7 years ago

@galakt, it's very probable that there's some sort of problem with your setup, rather than with Npgsql. Connection refused simply means that there's no one listening at the port you're trying to connect to, i.e. no PostgreSQL running on port 5432 - check that your docker setup is correct. You can confirm by trying to connect from the commandline with psql - if that works, Npgsql should work as well.

Closing for now but feel free to continue posting questions etc.

mota57 commented 6 years ago

@roji I did what the first steps say at http://www.npgsql.org/efcore/index.html. and I still get the connection refuse. I used psql and I created a database and a user. (in this case 'hmota').

Do I have to make another configuration with docker? I am running linux 16.04 with dotnet 2.0 and also I have postgres v10 with docker.

"ConnectionStrings": { "ApplicationContext": "Server=127.0.0.1;Port=5432;Database=sportstore;User Id=hmota; Password=123;"}

roji commented 6 years ago

Is your PostgreSQL running inside a docker container, and are you trying to connect to it from the outside? If so, are you exposing port 5432, and is networking set up correctly?

You should get to a point where you're able to simply telnet into port 5432 - that will show that networking is set up. Before that happens it really has nothing to do with Npgsql specifically.

agrothe commented 5 years ago

In case anyone else comes across this issue and lands here via a google search as I did, here are the specific steps I took to resolve:

In your Dockerfile add:

EXPOSE 5432

Then, (via stackexchange) add or edit the following line in your postgresql.conf :

listen_addresses = '*'

Add the following line as the first line of pg_hba.conf. It allows access to all databases for all users with an encrypted password:

# TYPE DATABASE USER CIDR-ADDRESS  METHOD
host  all  all 0.0.0.0/0 md5

Restart Postgresql after adding this with service postgresql restart or the equivalent command for your setup.

Now, in you appsettings.json, use the local IP address of you computer

  "ConnectionStrings": {
    "DefaultConnection": "Server=192.168.0.1;Port=5432;Database=<db>;User Id=<user>;Password=<userpass>;"
  },
wgrs commented 1 month ago

@galakt, it's very probable that there's some sort of problem with your setup, rather than with Npgsql. Connection refused simply means that there's no one listening at the port you're trying to connect to, i.e. no PostgreSQL running on port 5432 - check that your docker setup is correct. You can confirm by trying to connect from the commandline with psql - if that works, Npgsql should work as well.

Closing for now but feel free to continue posting questions etc.

Thats not my experience. I can ping the server but it wont accept a connection and "dotnet ef database update" fails to run with all sorts of bizarre error messages.

roji commented 1 month ago

The ability to ping doesn't necessarily mean much - ping uses ICMP whereas PostgreSQL uses TCP port 5432 (which may be blocked by some firewall. I'd suggest trying to connect with another PG client (e.g. the psql command-line utility) from the same machine/docker container.

In any case, Npgsql just establishes a TCP connection, and is getting a connection refused here - this is very unlikely to have anything to do with Npgsql.

wgrs commented 1 month ago

The ability to ping doesn't necessarily mean much - ping uses ICMP whereas PostgreSQL uses TCP port 5432 (which may be blocked by some firewall. I'd suggest trying to connect with another PG client (e.g. the psql command-line utility) from the same machine/docker container.

In any case, Npgsql just establishes a TCP connection, and is getting a connection refused here - this is very unlikely to have anything to do with Npgsql.

Should it just be a matter of adding postgres to the compose file and it should work, or is their additional configuration I am missing?

roji commented 1 month ago

@wgrs I'm no docker/docker-compose expert - try asking on stackoverflow or docker forums.