docker-compose up: FATAL: database "boilerplate," does not exist (Postgres Node) - node.js

I am trying to connect a node server to a postgres database with node-postgres (pg). My docker-compose.yaml is below:
version: "3.7"
services:
server:
build:
context: ./node-boilerplate
dockerfile: Dockerfile
ports:
- '3000:3000'
depends_on:
- db
environment:
DB_USER: postgres
DB_PASSWORD: postgres
DB_DATABASE: boilerplate,
DB_HOST: db
db:
image: postgres:latest
container_name: boiler-db
ports:
- '5432:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: boilerplate
When the node app tries to access a database called boilerplate that actually exists, it gives the error:
FATAL: database "boilerplate," does not exist
However, when I connect to the postgres container (using docker exec -it <postgres_container_ID> psql -U postgres) and list databases with \l, it shows the database boilerplate listed.
What am I missing? How come the database shows up but the node server cannot find it?
Thank you in advance.

Try to docker-compose up without comma:
DB_DATABASE: boilerplate

Related

Docker postgres connection refused when trying to pg_restore

Internal connection between my Spring Boot app and postgres db works fine but when I want to connect to db through init_database.sh and pg_restore I get pg_restore: error: connection to server at "db" (192.168.224.2), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?. In this .sh script I use the same host and port as in application.properties (where connection works fine as I said before).
I simply want to do a pg_restore from init_database.sh in my Docker container.
docker-compose.yml
services:
db:
container_name: db
hostname: db
image: postgres:14-alpine
command: -c 'max_connections=250'
ports:
- "5430:5432"
environment:
POSTGRES_DB: endlessblow_db
POSTGRES_USER: kuba
POSTGRES_PASSWORD: pass
volumes:
- ./init.dump:/init.dump
- ./init_database.sh:/docker-entrypoint-initdb.d/init_database.sh
restart: always
app:
container_name: app
image: 'endlessblow-server:latest'
build: ./
ports:
- "8000:8080"
depends_on:
- db
init_database.sh
#!/bin/sh
pg_restore --no-privileges --no-owner -h db -p 5432 -U kuba -d endlessblow_db init.dump
application.properties
spring.datasource.jdbc-url=jdbc:postgresql://db:5432/endlessblow_db
spring.datasource.url=jdbc:postgresql://db:5432/endlessblow_db
spring.datasource.username=kuba
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.platform=postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
How to solve that? Thank you!
in config of service "app", try following:
app:
...
depends_on:
- db
links:
- db
without links, docker network will isolate containers without explicitly exposing them to one another.

Unable to connect to the Postgres database in Docker Compose

I am building a Node JS Web application. I am using Postgres SQL and Sequelize for the database.
I have the docker-compose.yaml with the following content.
version: '3.8'
services:
web:
container_name: web-server
build: .
ports:
- 4000:4000
restart: always
depends_on:
- postgres
postgres:
container_name: app-db
image: postgres:11-alpine
ports:
- '5431:5431'
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=babe_manager
volumes:
- ./postgres-data:/var/lib/postgresql/data
build:
context: .
dockerfile: ./pgconfig/Dockerfile
volumes:
app-db-data:
I am using the following credentials to connect to the database.
DB_DIALECT=postgres
DB_HOST=postgres
DB_NAME=babe_manager
DB_USERNAME=docker
DB_PORT=5431
DB_PASSWORD=secret
When the application tries to connect to the database, I am getting the following error.
getaddrinfo ENOTFOUND postgres
I tried using this too.
DB_DIALECT=postgres
DB_HOST=localhost
DB_NAME=babe_manager
DB_USERNAME=docker
DB_PORT=5431
DB_PASSWORD=secret
This time I am getting the following error.
connect ECONNREFUSED 127.0.0.1:5431
My app server is not within the docker-compose.yaml. I just start it using "nodemon server.js". How can I fix it?
You need to make sure your Postgres container and your Node are on the same network. After they reside in the common network you can then use docker's DNS to resolve the database from the Node application in your case eighter postgres or test-db for hostname should work. More on container communication over docker networks.

ECONNREFUSED 127.0.0.1:5432

I am a beginner in node, nest, and docker but somehow I got assigned a job to dockerized all the existing node js applications.
I followed one of the youtube tutorial and successfully deployed the basic hello world via docker but in the next youtube tutorial when I am trying to add Postgres to the docker I am facing some issues in connecting to Postgres.
I am using docker desktop on mac.
Here is my docker-compose.yml file code snippet
version: "3.9" # optional since v1.27.0
services:
api:
build:
dockerfile: Dockerfile
context: .
depends_on:
- postgres
environment:
DATABASE_URL: postgres://user:password#postgres:5432/db
NODE_ENV: developement
PORT: 3000
ports:
- "8080:3000"
postgres:
image: postgres:14.0
ports:
- "35000:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
Here is the entire error log
Github Repository of this project
Thank you for helping in advance :)
Your problem for a typo in DATABASE_URL. In code for connect database use DATABSE_URL word but in docker-compose used DATABASE_URL.
You should change url: process.env.DATABSE_URL to url: process.env.DATABASE_URL
Make sure your connection string is correct in your docker-compose.yml. Just pass the host, port, user and pass seperated and let TypeOrm handle the connection.
// app.module.ts
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
})
And your docker-compose.yml:
# docker-compose.yml
version: '3.9'
services:
api:
build:
dockerfile: Dockerfile
context: .
depends_on:
- postgres
environment:
- POSTGRES_HOST=postgres
- POSTGRES_PASSWORD=promo-pass
- POSTGRES_USER=promo-user
- POSTGRES_DB=promo-api-db
- POSTGRES_PORT=5432
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: promo-user
POSTGRES_PASSWORD: promo-pass
POSTGRES_DB: promo-api-db
In the normal case, without Docker, i. e. you are using node and postgresql on you development or production machine, you just want to start postgres service and enable it if you want.
In order to start your postgres service, type the following command:
sudo systemctl start postgresql
To enable :
sudo systemctl enable postgresql
Note:
"Enable" means enbable the postgres server at boot time.
In my environment, i am using Red Hat. So if commands doesn't work, find the corresponding command on your linux distribution or on your specific OS.
I hope this can help someone else!

Docker Postgres service doesn't allow me to connect to it

I created a docker-composer.yml file in which I added a Postgres service (v10.6). Everything goes smooth, but when I'm trying to connect ti it with the User: postgres and Password: postgres I get the following error:
psql: FATAL: password authentication failed for user "docker"
My code is:
version: '2'
services:
postgres:
container_name: "egn-postgres"
image: "postgres:10.6"
restart: always
ports:
- "${DB_PORT}:${DB_PORT}"
What can I do?
I think you forgot to put the environment variables, try this
version: '2'
services:
postgres:
container_name: "egn-postgres"
image: "postgres:10.6"
restart: always
ports:
- "${DB_PORT}:${DB_PORT}"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
As #RickoThePow says, you have to define the environment variables.
You can use the solution presented by him:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
or use a separated file (postgres.env) with:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
and then invoke the file in docker-compose file like:
version: '2'
services:
postgres:
container_name: "egn-postgres"
image: "postgres:10.6"
.
.
.
env_file: postgres.env

NodeJS: Can't connect to postgreSQL container from another container

I have container with a NodeJS application and it has to be linked to another container with a Postgres db. My docker-compose.yml is:
version: "3"
services:
postgres:
image: "postgres:9.5.6-alpine"
container_name: postgres
volumes:
- /shared_folder/postgresql:/var/lib/postgresql
ports:
- "5432:5432"
web:
build: .
container_name: web
ports:
- "3000:3000"
links:
- postgres
depends_on:
- postgres
command: sh /usr/home/freebsd/wait-for-command.sh -c 'nc -z postgres 5432'
command: sh start.sh
On postgres container there is a db named "bucket", owned by user "user" with password "password".
When I type docker-compose up, and try to link the containers, after this message from console:
[INFO] console - postgres://user:password#localhost:5432/bucket
I get this error:
Error: Connection terminated
at [object Object].<anonymous>
in /node_modules/massive/node_modules/deasync/index.js:46
where is the problem?
You define command twice, which means your wait-for-command.sh is being overridden by sh start.sh and it's probably being run before PostgreSQL is ready to accept connections.

Resources