postgres database connected but table relations not found when running nodejs typescript api in a docker container

i wrote a basic api with typescript in nodejs which runs fine when started with the start script “node dist/index.js”, but after dockerizing it, the database seems to have been connected although there are no tables accessible

server logs

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[7/10/2024, 1:26:07 PM] [INFO] [0] [index.ts] Server is running on http://localhost:3000
[7/10/2024, 1:26:13 PM] [INFO] [0] [db.config] Connected to the db
[7/10/2024, 1:26:13 PM] [ERROR] [0] [operator.signup.controller] error: relation "operators" does not exist
at /app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
length: 108,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '13',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1392',
routine: 'parserOpenTable'
}
</code>
<code>[7/10/2024, 1:26:07 PM] [INFO] [0] [index.ts] Server is running on http://localhost:3000 [7/10/2024, 1:26:13 PM] [INFO] [0] [db.config] Connected to the db [7/10/2024, 1:26:13 PM] [ERROR] [0] [operator.signup.controller] error: relation "operators" does not exist at /app/node_modules/pg-pool/index.js:45:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { length: 108, severity: 'ERROR', code: '42P01', detail: undefined, hint: undefined, position: '13', internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'parse_relation.c', line: '1392', routine: 'parserOpenTable' } </code>
[7/10/2024, 1:26:07 PM] [INFO] [0] [index.ts]  Server is running on http://localhost:3000
[7/10/2024, 1:26:13 PM] [INFO] [0] [db.config]  Connected to the db
[7/10/2024, 1:26:13 PM] [ERROR] [0] [operator.signup.controller] error: relation "operators" does not exist
    at /app/node_modules/pg-pool/index.js:45:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  length: 108,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '13',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '1392',
  routine: 'parserOpenTable'
}

but when i logged in to docker container and connected to pg, the tables actually do exist.
docker container for pg database

github – https://github.com/aditipolkam/quicktrip

Dockerfile – using docker multi-stage build

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Stage 1: PostgreSQL
FROM postgres:15.5 AS db
COPY init.sql /docker-entrypoint-initdb.d/
EXPOSE 5432
# Stage 2: Node.js build
FROM node:20.5 AS build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
# Stage 3: Node.js production
FROM node:20.5 AS production
WORKDIR /app
COPY ssl-tls-key.pem ssl-tls-key.pem
COPY package*.json .
RUN npm install --only=production
ARG NODE_ENV
COPY .env.${NODE_ENV} .env
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]
</code>
<code># Stage 1: PostgreSQL FROM postgres:15.5 AS db COPY init.sql /docker-entrypoint-initdb.d/ EXPOSE 5432 # Stage 2: Node.js build FROM node:20.5 AS build WORKDIR /app COPY package*.json . RUN npm install COPY . . RUN npm run build # Stage 3: Node.js production FROM node:20.5 AS production WORKDIR /app COPY ssl-tls-key.pem ssl-tls-key.pem COPY package*.json . RUN npm install --only=production ARG NODE_ENV COPY .env.${NODE_ENV} .env COPY --from=build /app/dist ./dist CMD ["node", "dist/index.js"] </code>
# Stage 1: PostgreSQL
FROM postgres:15.5 AS db
COPY init.sql /docker-entrypoint-initdb.d/
EXPOSE 5432

# Stage 2: Node.js build
FROM node:20.5 AS build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build

# Stage 3: Node.js production
FROM node:20.5 AS production
WORKDIR /app
COPY ssl-tls-key.pem ssl-tls-key.pem
COPY package*.json .
RUN npm install --only=production
ARG NODE_ENV
COPY .env.${NODE_ENV} .env
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]

docker-compose.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>services:
db:
build:
context: .
target: db
env_file:
- .env.${NODE_ENV}
ports:
- "5432:5432"
volumes:
- db_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
interval: 30s
timeout: 10s
retries: 5
app:
build:
context: .
target: production
args:
NODE_ENV: ${NODE_ENV}
depends_on:
- db
ports:
- "3000:3000"
env_file:
- .env.${NODE_ENV}
environment:
- NODE_ENV=${NODE_ENV}
volumes:
db_data:
</code>
<code>services: db: build: context: . target: db env_file: - .env.${NODE_ENV} ports: - "5432:5432" volumes: - db_data:/var/lib/postgresql/data - ./init.sql:/docker-entrypoint-initdb.d/init.sql healthcheck: test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"] interval: 30s timeout: 10s retries: 5 app: build: context: . target: production args: NODE_ENV: ${NODE_ENV} depends_on: - db ports: - "3000:3000" env_file: - .env.${NODE_ENV} environment: - NODE_ENV=${NODE_ENV} volumes: db_data: </code>
services:
  db:
    build:
      context: .
      target: db
    env_file:
      - .env.${NODE_ENV}
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
      interval: 30s
      timeout: 10s
      retries: 5

  app:
    build:
      context: .
      target: production
      args:
        NODE_ENV: ${NODE_ENV}
    depends_on:
      - db
    ports:
      - "3000:3000"
    env_file:
      - .env.${NODE_ENV}
    environment:
      - NODE_ENV=${NODE_ENV}

volumes:
  db_data:

init.sql

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TABLE test_table (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE operators (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
contact INT NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
</code>
<code>CREATE TABLE test_table ( id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL ); CREATE TABLE operators ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, contact INT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL ); </code>
CREATE TABLE test_table (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);

CREATE TABLE operators (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    contact INT NOT NULL,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL
);

build and start docker containers – NODE_ENV=local docker-compose up --build -d

NODE_ENV can change – local | staging

.env.local

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>POSTGRES_USER=postgres
POSTGRES_HOST=db #was localhost earlier but changed to db for docker container execution
POSTGRES_PASSWORD=postgresdb
POSTGRES_DB=quicktrip
POSTGRES_PORT=5432
SALT_ROUNDS=10
</code>
<code>POSTGRES_USER=postgres POSTGRES_HOST=db #was localhost earlier but changed to db for docker container execution POSTGRES_PASSWORD=postgresdb POSTGRES_DB=quicktrip POSTGRES_PORT=5432 SALT_ROUNDS=10 </code>
POSTGRES_USER=postgres
POSTGRES_HOST=db      #was localhost earlier but changed to db for docker container execution  
POSTGRES_PASSWORD=postgresdb
POSTGRES_DB=quicktrip
POSTGRES_PORT=5432
SALT_ROUNDS=10

New contributor

Aditi Polkam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật