Home > Blockchain >  psql not connecting for timescaleDB container when working with Dockerfile
psql not connecting for timescaleDB container when working with Dockerfile

Time:01-23

I am working on timescaledb in Docker.

My Dockerfile is:

# Pull in the latest TimescaleDB image
FROM timescale/timescaledb:latest-pg14

RUN psql -U postgres -c "CREATE TABLE IF NOT EXISTS raw_table ...

I am getting this error at the last line:

#0 0.192 psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
#0 0.192    Is the server running locally and accepting connections on that socket?

I checked some solutions like making a wait_for_it.sh and give the psql some time to develop, but it is not working (does not sound like a good plan either).

I also looked at some similar problems like this, but I am not sure if it is exactly what I am looking for. It gave this solution:

docker run -p 5432:5432 -v /var/run/postgresql:/var/run/postgresql -d --name postgres postgres

So, to emulate it in docker-compose.yml (I need to use it), what I did is:

db:
    build: 
      context: 'timescaleDB/'
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=password
    volumes:
      - /var/run/postgresql:/var/run/postgresql

But, it did not solve the error.

CodePudding user response:

Take a look at how the timescale/timescaledb docker image does its own initialization of its database. You should be able to add your initialization code in /docker-entrypoint-initdb.d/002_custom_db_setup.sh

Your docker file would then consist of:

# Pull in the latest TimescaleDB image
FROM timescale/timescaledb:latest-pg14

COPY 002_custom_db_setup.sh /docker-entrypoint-initdb.d/002_custom_db_setup.sh

and 002_custom_db_setup.sh would be a bash script where you call psql :

#!/usr/bin/env bash

psql -U postgres -c "CREATE TABLE IF NOT EXISTS raw_table ..."
  • Related