I coded a simple server that uses postgres as DB. It was working when It's not using docker but when it's dockerized and orchestrated using docker compose, I couldn't insert data in because table doesn't not exist. I did check the DB in the container and it was there.
The following are my setup and the last was the error. Hopefully to you guys can spot something that I missed. Thanks!
docker-compose.yml
db:
build: ./database
ports:
- "5432:5432" # Bind host port 5432 to PostgreSQL port 5432
web:
build: ./app
command: npm start
ports:
- "8080:8080"
links:
- db
- redis
environment:
DB_URL: "postgres://dockeruser:@db:5432/docker?encoding=unicode&schema=public"
volumes:
- ./app:/src/*
database init
CREATE USER dockeruser WITH PASSWORD 'docker';
CREATE DATABASE docker;
CREATE TABLE people ( id SERIAL, name TEXT, email TEXT );
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dockeruser
code
pg.connect(conString, function(err, client, done) {
client.query('INSERT INTO public."people" (name, email) VALUES ($1, $2) RETURNING id', row, function(err, result) {
//call `done()` to release the client back to the pool
done();
if (err) return reject(err);
// console.log(result.rows[0].id);
resolve(result.rows[0].id);
});
});
error
db_1 | ERROR: relation "public.people" does not exist at character 13
db_1 | STATEMENT: INSERT INTO public."people" (name, email) VALUES ($1, $2) RETURNING id
web_1 | error { [error: relation "public.people" does not exist]
Dockerfileused for thedbservice.