title: Docker: ERROR: relation "users" does not exist at character 13
body: <p>I am trying to dockerize my app made with Python and FastAPI. I successfully created the images and container.</p>
  <p>I tried to dockerize my postgres database it was done successfully until I try to create a new user. It throws the following error:</p>
  <pre><code> ERROR:  relation &quot;users&quot; does not exist at character 13
  10 02:24:37.586 UTC [71] STATEMENT:  INSERT INTO users (email, password) VALUES ('sumitdadwal11@gmail.com', '$2b$12$VNya4IkKSGCuapswkJrh3u6POJVsdU2GSeIaV/ya4GprxNqEt5oim') RETURNING users.id
  </code></pre>
  <p>It shows the following error in my FastAPI app image:</p>
  <pre><code> Traceback (most recent call last):
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1802, in _execute_context
  
      self.dialect.do_execute(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py&quot;, line 719, in do_execute
  
      cursor.execute(statement, parameters)
  
  psycopg2.errors.UndefinedTable: relation &quot;users&quot; does not exist
  
  LINE 1: INSERT INTO users (email, password) VALUES ('sumitdadwal11@g...
  
                      ^
  
      The above exception was the direct cause of the following exception:
  
  
  Traceback (most recent call last):
  
    File &quot;/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py&quot;, line 376, in run_asgi
  
      result = await app(self.scope, self.receive, self.send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 75, in __call__
  
      return await self.app(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/fastapi/applications.py&quot;, line 208, in __call__
  
      await super().__call__(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/applications.py&quot;, line 112, in __call__
  
      await self.middleware_stack(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
  
      raise exc
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
  
      await self.app(scope, receive, _send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py&quot;, line 84, in __call__
  
      await self.app(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/exceptions.py&quot;, line 82, in __call__
  
      raise exc
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/exceptions.py&quot;, line 71, in __call__
  
      await self.app(scope, receive, sender)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/routing.py&quot;, line 656, in __call__
  
      await route.handle(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/routing.py&quot;, line 259, in handle
  
      await self.app(scope, receive, send)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/routing.py&quot;, line 61, in app
  
      response = await func(request)
  
    File &quot;/usr/local/lib/python3.9/site-packages/fastapi/routing.py&quot;, line 226, in app
  
      raw_response = await run_endpoint_function(
  
    File &quot;/usr/local/lib/python3.9/site-packages/fastapi/routing.py&quot;, line 161, in run_endpoint_function
  
      return await run_in_threadpool(dependant.call, **values)
  
    File &quot;/usr/local/lib/python3.9/site-packages/starlette/concurrency.py&quot;, line 39, in run_in_threadpool
  
      return await anyio.to_thread.run_sync(func, *args)
  
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/to_thread.py&quot;, line 28, in run_sync
  
      return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
  
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py&quot;, line 818, in run_sync_in_worker_thread
  
      return await future
  
    File &quot;/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py&quot;, line 754, in run
  
      result = context.run(func, *args)
  
    File &quot;/usr/src/app/./app/routers/user.py&quot;, line 18, in create_user
  
      db.commit()
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 1428, in commit
  
      self._transaction.commit(_to_root=self.future)
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 829, in commit
  
      self._prepare_impl()
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 808, in _prepare_impl
  
      self.session.flush()
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 3345, in flush
  
      self._flush(objects)
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 3485, in _flush
  
      transaction.rollback(_capture_exception=True)
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py&quot;, line 70, in __exit__
  
      compat.raise_(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py&quot;, line 207, in raise_
  
      raise exception
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 3445, in _flush
  
      flush_context.execute()
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 456, in execute
  
      rec.execute(self)
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 630, in execute
  
      util.preloaded.orm_persistence.save_obj(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/persistence.py&quot;, line 244, in save_obj
  
      _emit_insert_statements(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/persistence.py&quot;, line 1221, in _emit_insert_statements
  
      result = connection._execute_20(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1614, in _execute_20
  
      return meth(self, args_10style, kwargs_10style, execution_options)
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py&quot;, line 325, in _execute_on_connection
  
      return connection._execute_clauseelement(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1481, in _execute_clauseelement
  
      ret = self._execute_context(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1845, in _execute_context
  
      self._handle_dbapi_exception(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 2026, in _handle_dbapi_exception
  
      util.raise_(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py&quot;, line 207, in raise_
  
      raise exception
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1802, in _execute_context
  
      self.dialect.do_execute(
  
    File &quot;/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py&quot;, line 719, in do_execute
  
      cursor.execute(statement, parameters)
  
  sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation &quot;users&quot; does not exist
  
  LINE 1: INSERT INTO users (email, password) VALUES ('sumitdadwal11@g...
  
                      ^
  
  
  [SQL: INSERT INTO users (email, password) VALUES (%(email)s, %(password)s) RETURNING users.id]
  
  [parameters: {'email': 'sumitdadwal11@gmail.com', 'password': '$2b$12$5tcxP4b0hwVJpmHfyF10wuosYsdIxBkm1nhk1b1BZlLFZyCymodhK'}]
  
  (Background on this error at: https://sqlalche.me/e/14/f405)
  </code></pre>
  <p>This is my docker-compose.yml file:</p>
  <pre><code>   version: &quot;3&quot;
  services: 
    api:
      build: .
      ports:
        - 8000:8000
      # env_file:
      #      ./.env
      environment:
        - DATABASE_HOSTNAME=postgres
        - DATABASE_PORT=5432
        - DATABASE_PASSWORD=password123
        - DATABASE_NAME=fastapi
        - DATABASE_USERNAME=postgres
        - SECRET_KEY=secretkeysecretkeysecretkeysecretkey
        - ALGORITHM=HS256
        - ACCESS_TOKEN_EXPIRE_MINUTES=30
    postgres:
      image: postgres
      environment:
        - POSTGRES_PASSWORD=password123
        - POSTGRES_DB=fastapi
      
      volumes:
        - postgres-db:/var/lib/postgresql/data
  
  volumes:
    postgres-db:
  </code></pre>
  <p>This is my Dockerfile:</p>
  <pre><code>  FROM python:3.9.9
  
  WORKDIR /usr/src/app
  
  COPY requirements.txt ./
  
  RUN pip install --no-cache-dir -r requirements.txt
  
  COPY . .
  
  CMD [&quot;uvicorn&quot;, &quot;app.main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]
  </code></pre>
  <p>First I thought somethings wrong with my code but it is working fine when I am running it locally on my system.</p>
  <p>Also, I am very new to docker.</p>
  <p>I have tried different online solution but nothing worked out.</p>
  <p>I think it getting connected to the wrong table named postgres but I cant find the reason behinf it.</p>
  <p>UPDATE:
  What I have noticed is that my postgress server has three databases:
  -fastapi
  -fastapi-project</p>
  <ul>
  <li>postgres
  fastapi is the one i want to connect to but i think it is getting connected to the postgres one and I dont remember creating that database.
  Also the postgres database has no tables so it makes sense that it cannot find the user table.
  But the question is how do I change databases?</li>
  </ul>
  <p>Please let me know if you need any additional information.</p>
  <p>Thanks in advance!</p>