### PostgreSQL Environment Variables (.env) Example Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md This .env file demonstrates how to externalize PostgreSQL configuration variables like POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB for use with Docker Compose, improving security and manageability. ```env POSTGRES_USER=username POSTGRES_PASSWORD=password ``` -------------------------------- ### Run Docker Compose for PostgreSQL Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Executes the `docker-compose up` command in the project's root directory. This command builds, creates, and starts all services defined in the `docker-compose.yml` file, including the PostgreSQL database. ```shell docker-compose up ``` -------------------------------- ### Add PostgreSQL Initialization Script via Volume Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Mounts an SQL script file into the container's initialization directory. This script will be executed automatically when the PostgreSQL database container starts for the first time. ```yaml volumes: # In this example, we share an init.sql script with the container # The init script will be executed when the database is first run - ${PWD}/init.sql:/docker-entrypoint-initdb.d/init.sql ``` -------------------------------- ### Integrate pgAdmin Service with Docker Compose Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Adds a pgAdmin service to the Docker Compose setup, providing a web-based UI for managing the PostgreSQL database. It specifies the image, port mapping, environment file, dependencies, network, and data volume for pgAdmin. ```yaml services: database: ... # details from above pgadmin: image: dpage/pgadmin4 ports: - 15433:80 env_file: - .env depends_on: - database networks: - postgres-network volumes: - ${PWD}/pgadmin-data/:/var/lib/pgadmin/ ``` -------------------------------- ### PostgreSQL Docker Compose Configuration with Comments Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md This YAML file defines a PostgreSQL service for Docker Compose. It specifies the PostgreSQL image, maps ports, and sets environment variables for user, password, and database name, with detailed line-by-line explanations. ```yaml services: # The name of our service is "database" # but you can use the name of your choice. # Note: This may change the commands you are going to use a little bit. database: # Official Postgres image from DockerHub (we use the last version) image: 'postgres:latest' # By default, a Postgres database is running on the 5432 port. # If we want to access the database from our computer (outside the container), # we must share the port with our computer's port. # The syntax is [port we want on our machine]:[port we want to retrieve in the container] # Note: You are free to change your computer's port, # but take into consideration that it will change the way # you are connecting to your database. ports: - 5432:5432 environment: POSTGRES_USER: username # The PostgreSQL user (useful to connect to the database) POSTGRES_PASSWORD: password # The PostgreSQL password (useful to connect to the database) POSTGRES_DB: default_database # The PostgreSQL default database (automatically created at first launch) ``` -------------------------------- ### Configure PostgreSQL User and Password in docker-compose.yml Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Defines the PostgreSQL user and password directly within the docker-compose.yml file. These environment variables are crucial for connecting to the database instance. ```yaml environment: POSTGRES_USER=username # The PostgreSQL user (useful to connect to the database) POSTGRES_PASSWORD=password # The PostgreSQL password (useful to connect to the database) ``` -------------------------------- ### Specify Environment File for Docker Compose Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Utilizes the `env_file` tag to declare an external environment file, typically named `.env`. This allows for cleaner management of sensitive or numerous environment variables. ```yaml env_file: - .env # The name of your environment file (the one at the repository root) ``` -------------------------------- ### Stop Docker Compose for PostgreSQL Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Executes the `docker-compose down` command to stop and remove all containers, networks, and volumes created by `docker-compose up`. This is recommended to prevent background processes. ```shell docker-compose down ``` -------------------------------- ### Persist PostgreSQL Data with Docker Volumes Source: https://github.com/felipewom/docker-compose-postgres/blob/main/README.md Configures a Docker volume to share a local folder with the container's PostgreSQL data directory. This ensures data persistence even if the container is removed or recreated. ```yaml # The `volumes` tag allows us to share a folder with our container # Its syntax is as follows: [folder path on our machine]:[folder path to retrieve in the container] volumes: # In this example, we share the folder `db-data` in our root repository, with the default PostgreSQL data path # It means that every time the repository is modifying the data inside # of `/var/lib/postgresql/data/`, automatically the change will appear in `db-data` # You don't need to create the `db-data` folder. Docker Compose will do it for you - ${PWD}/db-data/:/var/lib/postgresql/data/ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.