Docker和Postgres,无法访问表格-Postgres错误:关系“public.Casino”不存在

xqk2d5yq  于 2022-12-29  发布在  Docker
关注(0)|答案(1)|浏览(111)

试图让节点容器与postgres容器一起工作,但遇到了一些问题。节点容器的网站运行良好,但似乎无法访问数据库表。
但是当我把一个shell附加到postgres容器并连接到“bambam”数据库时,表看起来就在那里,我遗漏了什么呢?

坞站组成:

version: '3.8'
services:
  api:
    container_name: bamcino
    restart: always
    build: . 
    ports:
     - 3000:3000
    depends_on:
      - db
    links:
      - "db:database"

  db:
    container_name: postgres
    image: postgres
    ports:
      - 5432:5432
    volumes:
      - ./db:/docker-entrypoint-initdb.d/
    environment:
      - POSTGRES_PASSWORD=Hahahaha
      - POSTGRES_DB=bambam

我的数据库/初始化SQL

CREATE DATABASE bambam;
GRANT ALL PRIVILEGES ON DATABASE bambam TO postgres;
--
-- PostgreSQL database dump
--
SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: Article; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public."Article" (
    id integer NOT NULL,
    title text NOT NULL,
    .....

节点应用程序中使用的连接字符串:

const sql = postgres('postgres://postgres:Hahahaha@db:5432/bambam');

尝试运行节点映像时出错:

Listening on 0.0.0.0:3000
PostgresError: relation "public.Casino" does not exist
    at ErrorResponse (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/connection.js:765:26)
    at handle (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/connection.js:468:6)
    at Socket.data (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/connection.js:312:9)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at cachedError (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/query.js:171:23)
    at new Query (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/query.js:36:24)
    at sql (file:///app/node_modules/.pnpm/postgres@3.3.2/node_modules/postgres/src/index.js:111:11)
    at t (file:///app/server/chunks/_server.ts-4c8e41d5.js:5:89)
PostgresError: relation "public.Article" does not exist
g6ll5ycj

g6ll5ycj1#

我得到了它的工作,我以为表已创建,但他们没有。它也是错误的使用:创建数据库在init.sql文件中,因为docker-compose会处理它。

相关问题