0

I am still very new with Docker but I have a docker container running postgresql and I need to connect to it via a c# application.

I have tried using npgsql to connect to my db but get a connection actively refused error. I have also attempted using different ports but I am limited to what I can use because of my apartments network, either way they still returned the same error.

Is using npgsql acceptable for connecting to a db running in a container? Any related posts or examples to accomplish this would be much appreciated.

I have also attempted to connect to the db running bash in a separate container using psql -h host -p port db_name but was also unsuccessful.

Solved

Issue was based on my "docker-machine ip default" being set to 192.168.99.100, this restricted me from connecting using local host 127.0.0.1 or 0.0.0.0. Using the default ip solved all my connection issues through PGAdmin and PGSQL in C#.

Hope this helps anyone with the same issue.

  • follow [this](https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside) – Software Dev Mar 16 '18 at 20:08
  • Npgsql can be used to connect to PostgreSQL regardless of whether it's running in docker or not. You just need to make sure your PostgreSQL port is properly exposed from your docker container. A good test is to try to telnet into port 5432 (the default PG port) from the outside, if that works Npgsql should work as well. – Shay Rojansky Mar 16 '18 at 20:30

1 Answers1

0

You can follow this But a short code that might help you out :

docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p     5433:5432 postgres
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • I'll get back to you when I have tested that out, wont be available till tomorrow morning. One more question if you happen to know the answer. Is there any restrictions on ports that I can utilize? The majority of mine are closed but I am not sure if traffic through another port could potentially cause problems.Thanks! – TheMackBreezy Mar 17 '18 at 00:03
  • I replicated exactly what the example did with no success. I have attempted connecting from bash on a separate container as well as pgAdmin 3 using open ports on my network. I may have a buddy try it on a home network with port 5432 to see if that rules out possible issues with my own network. – TheMackBreezy Mar 18 '18 at 01:21
  • Was on spring break for the week but I finally found the issue, my docker-machine ip default was set to 192.168.99.100. This would not allow me to connect using 127.0.0.1 or 0.0.0.0 on my host machine (w10) unless I was connecting through psql on a virtual environment or separate container with postgres.. – TheMackBreezy Apr 03 '18 at 23:30