skip to Main Content

So here is the setup:
1. I have a django container, postgresql container, and an nginx proxy running on an ec2 instance
2. I have another postgresql container running on locally on an ubuntu server
3. I am trying to connect the django server to the local postgresql server just to read data

Configuration:
1. for django I have the configuration for the remote database
2. Here is the config. All variables are set in a .env file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': os.environ.get('DB_HOST'),
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASS')
    },
    'nlp_data': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': os.environ.get('NLP_DB_HOST'),
        'NAME': os.environ.get('NLP_DB_NAME'),
        'USER': os.environ.get('NLP_DB_USER'),
        'PASSWORD': os.environ.get('NLP_DB_PASS')
    }
}

Problems:
1. The postgresql container on my local server… What would be the host of this database? Is it the public IP of the ubuntu server? Not sure
2. Is there a better way to debug these network issues? I have been getting timeout errors (assuming this is because my django container is unable to reach my local db server)

I have tried using the public IP of my local ubuntu server but that did not solve anything or tell me anything new.

2

Answers


  1. Chosen as BEST ANSWER

    I solved this by forwarding my network traffic on port 5432 to my local ubuntu server


  2. Sounds like a firewall and/or container networking issue. You should be able to connect from the django host to the postgres host, and you need to make sure the postgres host is forwarding postgres traffic to the container.

    Since you didn’t indicate how you’re running the containers, I’ll assume you’re using docker on both. Also, since you didn’t indicate what OS you’re running on the ec2 instance, I’ll assume linux.

    Using docker, you can see that the postgres host is forwarding traffic like so:

    postgres@postgres-host:~$ docker ps
    

    And look for a line such as:

    1234abcd5678 postgres:13.1 "docker-entrypoint.s…" 13 hours ago Up 4 seconds 0.0.0.0:5432->5432/tcp postgres-1

    The 0.0.0.0:5432->5432/tcp part indicates that the postgres host is forwarding port 5432 (postgres default) traffic on all interfaces to the container.

    The other thing you should do is verify you can connect from your running django container to the postgres host. You can use the postgres CLI tool to do that:

    django@django-host:~$ docker exec -ti <django-container-name> psql -h <postgres-hostname> -U <username>
    

    The tool will prompt you for a password. You can also run the psql command from the django host. You might need to install the postgres client to make this work. If you’re using ubuntu/debian you can do that with sudo apt install postgresql-client.

    The host of the database for an external client will be the hostname/IP of the server that the container is running on.

    If the psql command times out, then I suggest you look at the firewall settings of the postgres host server and make sure that the django container host can reach the internet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search