1

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.

  • You can't `RUN psql` in a Dockerfile, for a couple of reasons (the database itself isn't running at that point, and its data wouldn't be persisted due to the image setup). Does a setup like [How to create User/Database in script for Docker Postgres](https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres) work for this particular image? – David Maze Jan 21 '23 at 17:26
  • @DavidMaze, I am not sure about the link you stated. However, I remember once I myself worked on timescaleDB from terminal and I did something like this to run the database: docker run -d --name blah -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg14 Can I do it with 'RUN'? – Khabbab Zakaria Jan 21 '23 at 17:34
  • A Dockerfile can't really connect to a database at all, and a Dockerfile that's built `FROM` a database image can't usually create a derived image with preloaded data. – David Maze Jan 21 '23 at 17:58

1 Answers1

2

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 ..."
Will Holtz
  • 677
  • 5
  • 17
  • It seems to work just fine, however, I have a flask app in the same docker-compose.yaml, It is trying to store some data in the database. I dont think they are connecting. In the logs for the db, I am seeing this: background worker "TimescaleDB Background Worker Scheduler" (PID 54) exited with exit code 1 shutting down database system is shut down done server stopped PostgreSQL init process complete; ready for start up. Is this a follow up problem? – Khabbab Zakaria Jan 23 '23 at 12:13
  • I do to know what the new issue says, but your solution works perfectly! Thanks! Again, the issue seems to be a headache, or a bug, I need to check it. – Khabbab Zakaria Jan 23 '23 at 13:16
  • 1
    The shutdown followed by ready for start up is expected behavior - see https://github.com/docker-library/postgres/blob/a510f1341bbf92c9e1cd38566110412110bbfbde/docker-entrypoint.sh#L312-L336. The entrypoint script initializes the database, but then shuts it down so that the main process (CMD) for the container can run. – Will Holtz Jan 23 '23 at 16:49
  • Yes you are right @Will Holtz, thanks for the link. Now it is completely clear. – Khabbab Zakaria Jan 24 '23 at 12:57