title: How to set up my nodejs backend via docker so that the database is built with the proper tables?
body: <p>This might be jumbled up so I'm going to discuss my whole set up accordingly as well as describe my goal.</p>
  <p>I'm developing an api using Node.js/Express. I've integrated docker so that when anyone tries to run the application they'd only need to run the command <code>docker compose up</code></p>
  <p>The way the command is set up is via a dockerfile + docker-compose.yml</p>
  <p>the docker-compose.yml contains the following</p>
  <pre><code>version: '3.4'
  
  services:
    db:
      image: postgres
      environment:
        POSTGRES_PASSWORD: password
        POSTGRES_USER: postgres
        POSTGRES_DB: mainDB
      volumes:
        - ./pgdata:/var/lib/postgresql/data
      ports:
        - '5432:5432'
    api:
      image: api
      depends_on: 
        - db
      build:
        context: .
        dockerfile: ./Dockerfile
      environment:
        NODE_ENV: production
      ports:
        - 3000:3000
      volumes:
        - .:/usr/src/app
  </code></pre>
  <p>This sets up the docker container to have both the api and the database running together but the issue arises when I try to connect to the database.</p>
  <p>I have an sql file that contains the following</p>
  <pre><code>DROP TABLE IF EXISTS fruits;
  
  CREATE TABLE fruits (
      fruitID INTEGER primary key,
      fruitName VARCHAR(255) NOT NULL,
      fruitColor VARCHAR(255),
  );
  </code></pre>
  <p>I'm currently unable to connect to the database via pgAdmin and the error I'm getting is that the database name I'm referencing doesn't exist (&quot;mainDB&quot;). When I enter into psql via my terminal it also doesn't show the existence of a database either.</p>
  <p>My overall confusion is this: how can I set this up so that when the command <code>docker compose up</code> is executed, the user is able to have both the api and the database running with the table created in the database as well?</p>
  <p>Is this feasible? From what I've seen on tutorials is people having these sql files and just copy and pasting the commands into the psql command line. What would be the point of having the database running in the <code>docker-compose.yml</code> file if I'd have to manually build it from my psql command line?</p>