DockerFile
FROM timescale/timescaledb:latest-pg12
# Set environment variables for PostgreSQL
ENV POSTGRES_USER=myuser
ENV POSTGRES_PASSWORD=mypassword
ENV POSTGRES_DB=mydb
# Copy initialization script into the container
COPY init.sql /docker-entrypoint-initdb.d/
# Expose the PostgreSQL port
EXPOSE 5432
you will need to create an init.sql script that contains the necessary SQL commands to create the desired login role, database, and any other customizations you might need. Here's an example init.sql script
init script
-- Create the user with the specified username and password
CREATE USER myuser WITH PASSWORD 'mypassword';
-- Create the database and grant privileges to the user
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
After creating the Dockerfile and the init.sql script, you can build the Docker image using the following command:
docker build -t my-timescaledb
Then, you can run the container and initialize the TimescaleDB by piping the init.sql script content to the psql command within the container:
cat init.sql | docker run -i --rm --name my-timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=mypassword my-timescaledb:latest-pg12
This approach creates a Docker image that automatically runs the SQL script during the container initialization, creating the specified user, database, and other customizations you might include in the init.sql script.