0

I am relatively new to this topic. I try to build a webapp using flask. The webapp uses data from a postgresql database which is running local (Mac OS Monterey 12.2.1).

My application uses a python code which accesses data from the database by connecting to the database with psycopg2:

con = psycopg2.connect(
            host = "192.168.178.43"
            database = self.database,
            port = "5432",
            user = "user",
            password = "password")

I already added the relevant entries to the "pg_hba.conf" file and to the "postgresql.conf" file to the needed configurations for an access in my home network. But i still got an error when starting the container. The app runs perfect outside the container. I think I miss some important steps to complete a successful connection.

This error is the following

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

M.J.1990
  • 11
  • 2
  • The database is running outside a container; you installed it with something like Homebrew? If you're using Docker Desktop the special host name `host.docker.internal` can reach it. – David Maze Apr 20 '22 at 13:11
  • thank you for that awnser. This will not work for me as my aim is, to run the webapp in a dockercontainer on my raspberry pi in the next step. I have the same database on my raspi and i would like to take the connection to that database later on. – M.J.1990 Apr 20 '22 at 13:20
  • But still outside a container? There's a canonical question [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) that discusses both the Docker Desktop and native-Linux cases. You could also run the database in a container, which might be easier to deploy and connect to. – David Maze Apr 20 '22 at 13:59
  • To give an awnser on the question above, I dealed with the problem by using a containerized database. I copied my database into a second container and let the two containers communicate by using docker-compose. – M.J.1990 May 01 '22 at 13:49

0 Answers0