Psql Not Connecting for timescaleDB Container When Working with Dockerfile

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.

3

1 Answer

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 ..."
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest