How can I connect to database running in docker-compose and deployed with ec2 instance?

I am new to Amazon AWS and have launched my first project. It is a simple django project, with postgres as a db, everything connected with the help of docker-compose. Here is the docker-compose file:

version: "3"

services:
  backend:
    build: ./
    container_name: ${PROJECT_NAME}
    working_dir: /${PROJECT_NAME}
    ports:
      - ${LOCAL_PORT}:${WSGI_PORT}
    volumes:
      - static_content:/${PROJECT_NAME}/src/static/
      - media_content:/${PROJECT_NAME}/src/media/
      - ./src:/${PROJECT_NAME}/src
    restart: always
    env_file:
      - .env

  postgres:
    image: postgres:12.0
    container_name: postgres
    ports:
      - "5432:5432"
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    restart: always
    env_file:
      - .env

  pgadmin:
    image: dpage/pgadmin4
    container_name: postgres_admin
    ports:
      - "5000:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    env_file:
      - .env
    restart: on-failure
    depends_on:
      - postgres

volumes:
  static_content: { }
  media_content: { }
  local_postgres_data: { }
  local_postgres_data_backups: { }
  pgadmin_data: { }

I have successfully uploaded the project to my EC2 instance, but the problem is that I cannot access the db with pgadmin. I tried connecting with the port, like this ec2-**-***-***-**.compute-1.amazonaws.com:5000, but it didn't work. Connecting with pgadmin app also didn't work as it gives me Unable to connect to server: connection to server at "***", port 5432 failed: timeout expired.

Security groups are as following: Security groups

And here is the DB config:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "course_project",
        "USER": "postgres",
        "PASSWORD": "admin",
        "HOST": "postgres",
        "PORT": "5432",
    }
}

The thing is that postgres and pgadmin containers starts after running docker-compose up in my ec2 instance. However, I can`t access them…

Are there any lifehacks on how can I finally connect to my database? Would be very grateful for every answer!

Upvotes: 0

Views: 888

Answers (1)

simar
simar

Reputation: 1822

Can access to pgadmin? Or u can't connect to database via pgadmin ? First ensure pgadmin is up and running

ssh youUser@ec2-**-***-***-**.compute-1.amazonaws.com

Ensure pgadmin container is up

docker ps | grep pgadmin 

Ensure pgadmin gives response (port configuration is correct)

curl -v http://localhost:5000 

should respond main page into console

At this moment you could try

ssh -L 5000:localhost:5000 ec2-**-***-***-**.compute-1.amazonaws.com

Now open on you local machine http://localhost:5000

If you able to see pgadmin console means everything correct. Only need to permit access on port 5000 to ec2----.compute-1.amazonaws.com inbound rule.

Anyway since you are using http better either use ssh passthrough(uppers example with -L), either enable tls in pgadmin.

Upvotes: 1

Related Questions