Dockerfile for PostgreSQL 16.1
TODO: collapasable, copyable, download, etc
I need to create a Dockerfile for running postgres:16.1 I would like run ./init.sql
in the image as it will setup the database. I would like to be able to override the deafult root password, etc. Can you give me a sample Dockerfile?Certainly! To create a Dockerfile for running PostgreSQL 16.1 and executing an init.sql
script during the setup, you'll also want to ensure that you can override the default database configurations such as the root password. Here is a sample Dockerfile and explanations for each step:
# Use the official PostgreSQL image as the base image
FROM postgres:16.1
# Set environment variables to override default values
# Replace these with your desired default values or pass them during container creation
ENV POSTGRES_PASSWORD=mysecretpassword
ENV POSTGRES_USER=myuser
ENV POSTGRES_DB=mydatabase
# Copy your init.sql script into the /docker-entrypoint-initdb.d directory
# Scripts in this directory are automatically executed during container startup
COPY ./init.sql /docker-entrypoint-initdb.d/
# No need to expose ports or CMD as they are inherited from the base image
Here is a breakdown of the Dockerfile:
Base Image: We start with the official PostgreSQL 16.1 image.
Environment Variables:
POSTGRES_PASSWORD
,POSTGRES_USER
,POSTGRES_DB
: These are the environment variables used by the PostgreSQL Docker image to set the default user, password, and database name. You can set them in the Dockerfile or override them at runtime using-e
flags when running the container.
Copy
init.sql
:- The script
init.sql
is copied into the/docker-entrypoint-initdb.d/
directory in the container. PostgreSQL executes scripts in this directory upon container initialization, allowing you to run your custom setup.
- The script
Inherited Configurations:
- The
EXPOSE
directive and the default command (CMD
) are inherited from the base image, so you don't need to specify them unless you want to change the defaults.
- The
To build and run your container with this Dockerfile:
Build the Docker Image:
docker build -t my-postgres .
Run the Container:
- To use the default environment variables specified in the Dockerfile:
docker run -d --name my-postgres-container -p 5432:5432 my-postgres
- To override environment variables:
docker run -d --name my-postgres-container -p 5432:5432 -e POSTGRES_PASSWORD=newpassword -e POSTGRES_USER=newuser -e POSTGRES_DB=newdb my-postgres
- To use the default environment variables specified in the Dockerfile:
This setup will create a PostgreSQL container with your init.sql
script executed and the ability to customize the root password and other settings.I have this init.sql I wrote for MySQL. Can you analyze it line by line and port it to Postgres?
-- Set utf8 settings globally SET collation_server = utf8_unicode_ci; SET character_set_server = utf8;
CREATE DATABASE IF NOT EXISTS appdata DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER IF NOT EXISTS 'deductive'@'%' IDENTIFIED BY 'fl4uVU#3';
-- Needed for dev, staging, and production GRANT ALL PRIVILEGES ON appdata.* TO 'deductive'@'%';
-- The following is needed only in dev (docker and the developer workflow) GRANT ALL PRIVILEGES ON appdata_shadow.* TO 'deductive'@'%';
FLUSH PRIVILEGES;
Certainly! Let's analyze and convert your MySQL initialization script (init.sql
) to be compatible with PostgreSQL. PostgreSQL has different syntax and methods for setting up databases and users.
MySQL Script Analysis and PostgreSQL Conversion
Setting UTF-8 Settings Globally
- MySQL:
SET collation_server = utf8_unicode_ci; SET character_set_server = utf8;
- PostgreSQL:
- PostgreSQL automatically uses UTF-8 encoding, but you can ensure it by setting the
LC_COLLATE
andLC_CTYPE
settings when creating the database. - PostgreSQL doesn't have global character settings like MySQL, so this step is usually not necessary.
- PostgreSQL automatically uses UTF-8 encoding, but you can ensure it by setting the
- MySQL:
Create Database
- MySQL:
CREATE DATABASE IF NOT EXISTS appdata DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
- PostgreSQL:
CREATE DATABASE appdata WITH ENCODING 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8' TEMPLATE template0;
- MySQL:
Create User
- MySQL:
CREATE USER IF NOT EXISTS 'deductive'@'%' IDENTIFIED BY 'fl4uVU#3';
- PostgreSQL:
CREATE USER deductive WITH PASSWORD 'fl4uVU#3';
- MySQL:
Grant All Privileges on Database
- MySQL:
GRANT ALL PRIVILEGES ON appdata.* TO 'deductive'@'%';
- PostgreSQL:
GRANT ALL PRIVILEGES ON DATABASE appdata TO deductive;
- MySQL:
Grant Privileges on appdata_shadow Database (Dev Environment)
- This implies you have another database named
appdata_shadow
for development purposes. - PostgreSQL:
CREATE DATABASE appdata_shadow WITH ENCODING 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8' TEMPLATE template0; GRANT ALL PRIVILEGES ON DATABASE appdata_shadow TO deductive;
- This implies you have another database named
Flush Privileges
- MySQL uses
FLUSH PRIVILEGES;
to reload the grant tables. - PostgreSQL automatically applies changes; there is no equivalent or need for
FLUSH PRIVILEGES
.
- MySQL uses
Final PostgreSQL Script
-- Create appdata database with UTF-8 settings
CREATE DATABASE appdata
WITH
ENCODING 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TEMPLATE template0;
-- Create user
CREATE USER deductive WITH PASSWORD 'fl4uVU#3';
-- Grant privileges on appdata
GRANT ALL PRIVILEGES ON DATABASE appdata TO deductive;
-- For development: Create appdata_shadow database and grant privileges
CREATE DATABASE appdata_shadow
WITH
ENCODING 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TEMPLATE template0;
GRANT ALL PRIVILEGES ON DATABASE appdata_shadow TO deductive;
Please ensure to adjust the LC_COLLATE and LC_CTYPE settings as per your locale or requirements. The script creates the databases appdata
and appdata_shadow
(for development), a user deductive
, and grants necessary privileges to this user on both databases.
Related Chats
Dockerfile for MySQL 8.0.34 0.564
MySQL Docker User Permissions 0.561
Env File for Docker 0.546
Update Docker Compose Envvars 0.455
Docker PostgreSQL Connection Issue 0.435
Deploying Docker Compose EC2 0.427
Docker ENV Variables Not Set 0.418
Auth0 Deployment Plan 0.397