Install Postgresql In Docker

  1. Install Postgresql Docker Mac
  2. Install Postgres In Centos Docker
  3. Postgresql Docker Volume
  4. Using Postgresql Docker Image

Tutorial PostgreSQL - Docker Installation on Ubuntu Linux. Install the Docker service. Copy to Clipboard. Apt-get update apt-get install docker.io. Download the PostgreSQL docker image from the online repository. Copy to Clipboard. Docker pull postgres. List the Docker images installed on your system. Copy to Clipboard.

Docker Install Postgresql Image. In the Services tool window, select the Compose node and click. Docker Install Postgres 12. In the Services tool window, select the Compose node and click. This stops and removes containers along with all related networks, volumes, and images. Docker pull Postgres is a simple way to install and run software on your local machine. The Docker pull command pulls down the latest stable release Postgres image from the official Postgres docker hub repository. Don’t install Postgres. Create a directory to serve as the local host point for Postgres Data Files.

Docker-compose exec postgres bash knowing that postgres is the name of the service. Replace it with the name of the Postgresql service in you docker-compose file. If you have many docker-compose files, you have to add the specific docker-compose.yml file you want to execute the command with. Use the following commnand instead. # Note that the first run could take a few minutes due to the image being downloaded docker run -d -name pg13 -p 5432:5432 -e POSTGRESHOSTAUTHMETHOD=trust postgres:13 # Connect to the container that’s been started and display the exact server version psql -U postgres -h localhost -p 5432 -c 'show serverversion' postgres serverversion.

We and third parties use cookies or similar technologies ('Cookies') as described below to collect and process personal data, such as your IP address or browser information. Docker build -build-arg POSTGRESMAJORVERSION=13 -build-arg POSTGISMAJOR=3 -t kartoza/postgis:POSTGRESMAJORVERSION. Alternative base distributions builds There are build args for DISTRO (=debian), IMAGEVERSION (=buster) and IMAGEVARIANT (=slim) which can be used to control the base image used (but it still needs to be Debian based.

Use this information to quickly start up Community Edition using Docker Compose.

Note: While Docker Compose is often used for production deployments, the Docker Compose file provided here is recommended for development and test environments only. Customers are expected to adapt this file to their own requirements, if they intend to use Docker Compose to deploy a production environment.

To deploy Community Edition using Docker Compose`, download and install Docker, then follow the steps below. Make sure that you’ve reviewed the prerequisites before continuing.

  1. Clone the project locally, change directory to the project folder, and switch to the release branch:

    Note: Make sure that exposed ports are open on your host computer. Check the docker-compose.yml file to determine the exposed ports - refer to the host:container port definitions. You’ll see they include 5432, 8080, 8083 and others.

  2. Save the docker-compose.yml file in a local folder.

    For example, you can create a folder docker-compose.

  3. Change directory to the location of your docker-compose.yml file.

  4. Deploy Community Edition, including the repository, Share, Postgres database, Search Services, etc.:

    This downloads the images, fetches all the dependencies, creates each container, and then starts the system:

    Note that the name of each container begins with the folder name you created in step 2.

    As an alternative, you can also start the containers in the background by running docker-compose up -d.

  5. Wait for the logs to complete, showing messages:

    See Troubleshooting if you encounter errors whilst the system is starting up.

  6. Open your browser and check everything starts up correctly:

    ServiceEndpoint
    Administration and REST APIshttp://localhost:8080/alfresco
    Sharehttp://localhost:8080/share
    Search Services administrationhttp://localhost:8083/solr

    If Docker is running on your local machine, the IP address will be just localhost.

    If you’re using the Docker Toolbox, run the following command to find the IP address:

  7. Log in as the admin user. Enter the default administrator password admin.

Check system start up

Use this information to verify that the system started correctly, and to clean up the deployment.

  1. Open a new terminal window.

  2. Change directory to the docker-compose folder that you created in the deployment steps.

  3. Verify that all the services started correctly.

    1. List the images and additional details:

      You should see a list of the services defined in your docker-compose.yaml file:

    2. List the running containers:

      You should see a list of the services defined in the docker-compose.yaml file.

    3. View the log files for each service <service-name>, or container <container-name>:

      For example, to check the logs for Share, run any of the following commands:

      You can add an optional parameter --tail=25 before <container-name> to display the last 25 lines of the logs for the selected container.

      Check for a success message:

    Once you’ve tested the services, you can clean up the deployment by stopping the running services.

  4. Stop the session by using CONTROL+C in the same window as the running services:

  5. Alternatively, you can open a new terminal window, change directory to the docker-compose folder, and run:

    This stops the running services, as shown in the previous example, and removes them from memory:

  6. You can use a few more commands to explore the services when they’re running. Change directory to docker-compose before running these:

    1. Stop all the running containers:

    2. Restart the containers (after using the stop command):

    3. Starts the containers that were started with docker-compose up:

    4. Stop all running containers, and remove them and the network:

      The --rmi all option also removes the images created by docker-compose up, and the images used by any service. You can use this, for example, if any containers fail and you need to remove them:

See the Docker documentation for more on using Docker.

Deployment project in GitHub

See the Alfresco/acs-deployment GitHub project for more details.

  • In this project, you’ll find several Docker Compose files. The default docker-compose.yml file contains the latest work-in-progress deployment scripts, and installs the latest development version of Content Services.
  • To deploy a specific released version of Content Services, several major.minor Docker Compose files are provided in the docker-compose folder of the project.
  • To modify your development environment, for example to change or mount files in the existing images, you’ll have to create new custom Docker images (recommended approach). The same approach applies if you want to install AMP files into the repository and Share images. See the Customization guidelines for more.

Using the Community Compose file in this project deploys the following system:

Cleanup

To bring the system down and cleanup the containers run the following command:

Troubleshooting

  1. If you have issues running docker-compose up after deleting a previous Docker Compose cluster, try replacing step 4 in the initial Docker Compose instructions with:

    Note: Make sure that the docker-compose up part of the command uses the format you chose in step 4.

  2. Stop the session by using CONTROL+C.

  3. Remove the containers (using the --rmi all option):

  4. Try allocating more memory resources, as advised in docker-compose.yml.

    For example, in Docker, change the memory setting in Preferences (or Settings) Resources > Advanced > Memory to at least 8GB. Make sure you restart Docker and wait for the process to finish before continuing.

    Go back to step 4 in the initial Docker Compose instructions to start the deployment again.

Note: Keep in mind that 8GB is much lower than the required minimum, and may need to be adapted for your environment. You’ll need a machine with at least 13GB of memory to distribute among the Docker containers.

Docker enables developers to deploy applications inside containers for testing code in an environment identical to production. IntelliJ IDEA provides Docker support using the Docker plugin. The plugin is bundled and enabled by default in IntelliJ IDEA Ultimate Edition. For IntelliJ IDEA Community Edition, you need to install the Docker plugin as described in Manage plugins.

Enable Docker support

  1. Install and run Docker.

    For more information, see the Docker documentation.

  2. Configure the Docker daemon connection settings:

    • In the Settings/Preferences dialog Ctrl+Alt+S, select Build, Execution, Deployment Docker.

    • Click to add a Docker configuration and specify how to connect to the Docker daemon.

      The connection settings depend on your Docker version and operating system. For more information, see Docker configuration.

      The Connection successful message should appear at the bottom of the dialog.

      The Path mappings table is used to map local folders to corresponding directories in the Docker virtual machine's file system. Only specified folders will be available for volume binding.

      This table is not available on Linux, because when running Docker on Linux, any folder is available for volume binding.

  3. Connect to the Docker daemon.

    The configured Docker connection should appear in the Services tool window (View Tool Windows Services or Alt+8 ). Select the Docker node , and click , or select Connect from the context menu.

    To edit the Docker connection settings, select the Docker node and click on the toolbar, or select Edit Configuration from the context menu.

The Services tool window (View Tool Windows Services or Alt+8) enables you to pull and push images, create and run containers, manage Docker Compose, and so on. As with other tool windows, you can start typing the name of an image or container to highlight the matching items.

Managing images

Docker images are executable packages for running containers. Depending on your development needs, you can use Docker for the following:

Docker
  • Pull pre-built images from a Docker registry

    For example, you can pull an image that runs a Postgres server container to test how your application will interact with your production database.

  • Build images locally from a Dockerfile

    For example, you can build an image that runs a container with the Java Runtime Environment (JRE) of some specific version to execute your Java application inside it.

  • Push your images to a Docker registry

    For example, if you want to demonstrate to someone how your application runs in some specific version of the JRE instead of setting up the proper environment, they can run a container from your image.

Images are distributed via the Docker registry. Docker Hub is the default public registry with all of the most common images: various Linux flavors, database management systems, web servers, runtimes, and so on. There are other public and private Docker registries, and you can also deploy your own registry server.

You do not need to configure a registry if you are going to use only Docker Hub.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Build, Execution, Deployment Docker Registry.

  2. Click to add a Docker registry configuration and specify how to connect to the registry. If you specify the credentials, IntelliJ IDEA will automatically check the connection to the registry. The Connection successful message should appear at the bottom of the dialog.

  1. In the Services tool window, select the Images node.

  2. In the Images Console, type the name of the image to pull. Use completionCtrl+Space to select from the available suggestions of official images. Press Ctrl+Space again to see relevant images from all users.

  3. Press Ctrl+Enter to run docker pull.

Docker Install Postgres Client

When you are editing a Dockerfile, IntelliJ IDEA provides completion for images from the configured registries. You can also hold down Ctrl and click an image name to open its page in a web browser.

  1. Open the Dockerfile from which you want to build the image.

  2. Click in the gutter and select to build the image.

IntelliJ IDEA runs the docker build command.

  1. In the Services tool window, select the image that you want to upload and click or select Push Image from the context menu.

  2. Select the Docker registry and specify the repository and tag (name and version of the image, for example, my-app:v2 ).

  3. Click OK to run the docker push command.

Images that you pull or build are stored locally and are listed in the Services tool window under Images. When you select an image, you can view its ID or copy it to the clipboard by clicking the button on the Properties tab.

To display detailed information about an image, right-click it and select Inspect from the context menu. IntelliJ IDEA runs the docker image inspect command and prints the output to the Inspection tab.

Images with no tags <none>:<none> can be one of the following:

  • Intermediate images that serve as layers for other images and do not take up any space

  • Dangling images that remain when you rebuild an image based on a newer version of another image. You should regularly prune dangling images to preserve disk space.

To hide untagged images from the list, click on the Docker toolbar, and then click Untagged Images to remove the check mark.

To delete one or several images, select them in the list and click .

Running containers

Containers are runtime instances of corresponding images. For more information, see the docker run command reference.

IntelliJ IDEA uses run configurations (Run Edit Configurations) to run Docker containers. There are three types of Docker run configurations:

  • Docker Image: Created automatically when you run a container from an existing image. You can run it from a locally existing Docker image that you either pulled or built previously.

  • Dockerfile: Created automatically when you run a container from a Dockerfile. This configuration builds an image from the Dockerfile, and then derives a container from this image.

  • Docker-compose: Created automatically when you run a multi-container Docker application from a Docker Compose file.

Any Docker run configuration can also be created manually. From the main menu, select Run Edit Configurations. Then click , point to Docker, and select the desired type of run configuration.

  1. In the Services tool window, select an image and click or select Create Container from the context menu.

  2. In the Create container popup, click Create.

    If you already have a Docker run configuration for this image, the Create container popup will also contain the name of that run configuration as an option.

  3. In the Create Docker Configuration dialog that opens, you can provide a unique name for the configuration and specify a name for the container. If you leave the Container name field empty, Docker will give it a random unique name.

  4. When you are done, click Run to launch the new configuration.

  1. Open the Dockerfile from which you want to run the container.

  2. Click in the gutter and select to run the container on a specific Docker node.

This creates and starts a run configuration with default settings, which builds an image based on the Dockerfile and then runs a container based on this image.

To create a run configuration with custom settings, click in the gutter and select New Run Configuration. You can specify a custom tag for the built image, as well as a name for the container, and a context folder from which to read the Dockerfile. The context folder can be useful, for example, if you have some artifacts outside of the scope of your Dockerfile, which you would like to add to the file system of the image.

You can right-click the Dockerfile in the Project tool window for the following useful actions:

  • Run the container from the Dockerfile

  • Save the run configuration for the Dockerfile

  • Select the run configuration for this Dockerfile to make it active

Command-line options

When running a container on the command line, the following syntax is used:

All optional parameters can be specified in the corresponding Docker run configuration fields.

To open a run configuration, right-click a container and select Edit Configuration, or use the gutter icon menu in the Dockerfile, or select Run Edit Configurations from the main menu.

Options are specified in the Command line options field. In the previous screenshot, the container is connected to the my-net network and is assigned an alias my-app.

Commands and arguments to be executed when starting the container are specified in the Entrypoint and Command fields. These fields override the corresponding ENTRYPOINT and CMD instructions in the Dockerfile. In the previous screenshot, when the container starts, it executes the docker-entrypoint.sh script with postgres as an argument.

Not all docker run options are supported. If you would like to request support for some option, leave a comment in IDEA-181088.

The Command preview field shows the actual Docker command used for this run configuration.

You can also configure the following container settings in the run configuration:

Install postgresql docker mac

Bind mounts

Docker can mount a file or directory from the host machine to the container using the -v or --volume option. You can configure this in the Docker run configuration using the Bind mounts field.

Make sure that the corresponding path mappings are configured in the Docker connection settings (the Path mappings table).

Click in the Bind mounts field and add bindings by specifying the host directory and the corresponding path in the container where it should be mounted. Select Read only if you want to disable writing to the container volume. For example, if you want to mount some local PostgreSQL data directory (/Users/Shared/pg-data) to the PostgreSQL data directory inside the container (/var/lib/pgsql/data ), this can be configured as illustrated on the previous screenshot.

If you expand the Command preview field, you will see that the following line was added:

This can be used in the Command line options field instead of creating the list of volume bindings using the Bind Mounts dialog.

View and modify volume bindings for a running container

  1. In the Services tool window, select the container and then select the Volume Bindings tab.

  2. To create a new binding, click . To edit an existing one, select the binding and click .

  3. Specify the settings as necessary and click Save to apply the changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Bind ports

Docker can map specific ports on the host machine to ports in the container using the -p or --publish option. This can be used to make the container externally accessible. In the Docker run configuration, you can choose to expose all container ports to the host or use the Bind ports field to specify port mapping.

Click in the Bind ports field and bindings by specifying which ports on the host should be mapped to which ports in the container. You can also provide a specific host IP from which the port should be accessible (for example, you can set it to 127.0.0.1 to make it accessible only locally, or set it to 0.0.0.0 to open it for all computers in your network).

If you already have PostgreSQL running on the Docker host port %5432%, you can map port %5433% on the host to %5432% inside the container as illustrated on the previous screenshot. This will make PostgreSQL running inside the container accessible via port %5433% on the host.

If you expand the Command preview field, you will see that the following line was added:

This can be used in the Command line options field instead of creating the list of port bindings using the Port Bindings dialog.

View and modify port bindings for a running container

  1. In the Services tool window, select the container and then select the Port Bindings tab.

  2. To create a new binding, click . To edit an existing one, select the binding and click . If the Publish all ports checkbox is selected, clear it to be able to specify individual port mappings.

  3. Specify the settings as necessary and click Save to apply changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Environment variables

Environment variables are usually set in the Dockerfile associated with the base image that you are using. There are also environment variables that Docker sets automatically for each new container. You can specify additional variables and redefine the ones that Docker sets using the -e or --env option. In a Docker run configuration, you can use the Environment variables field to configure environment variables.

Click in the Environment variables field to add names and values for variables. For example, if you want to connect to PostgreSQL with a specific username by default (instead of the operating system name of the user running the application), you can define the PGUSER variable as illustrated on the previous screenshot.

If you expand the Command preview field, you will see that the following line was added:

This can be used in the Command line options field instead of creating the list of names and values using the Environment Variables dialog. If you need to pass sensitive information (passwords, secrets, and so on) as environment variables, you can use the --env-file option to specify a file with this information.

View and modify environment variables for a running container

  1. In the Services tool window, select the container and then select the Environment variables tab.

  2. To add a new variable, click . To edit an existing one, select the variable and click .

  3. Specify the settings as necessary and click Save to apply changes.

The container is stopped and removed, and a new container is created with the specified changes. However, changes are not saved in the corresponding run configuration.

Build-time arguments

Docker can define build-time values for certain environment variables that do not persist in the intermediate or final images using the --build-arg option for docker build. These must be specified in the ARG instruction of the Dockerfile with a default value. You can configure build-time arguments in the Docker run configuration using the Build args field.

For example, you can use build-time arguments to build the image with a specific version of PostgreSQL. To do this, add the ARG instruction to the beginning of your Dockerfile:

The PGTAG variable in this case will default to latest if you do not redefine it as a build-time argument. So by default, this Dockerfile will produce an image with the latest available PostgreSQL version. However, you can use the Build Args field to redefine the PGTAG variable.

In the previous screenshot, PGTAG is set to 9, which will instruct Docker to pull postgres:9. When you deploy this run configuration, it will build an image and run the container with PostgreSQL version 9.

To check this, execute postgres --version inside the container and see the output: it should be postgres (PostgreSQL) 9.6.6 or some later version.

If you expand the Command preview field, you will see that the following option was added to the docker build command:

Interacting with containers

Created containers are listed in the Services tool window. When you select a container, you can view its ID (and the ID of the corresponding image) and copy it to the clipboard using on the Properties tab. You can also specify a new name for the container and click Save to start another container with this new name from the same image.

By default, the Services tool window displays all containers, including those that are not running. To hide stopped containers from the list, click , and then click Show Stopped Containers to remove the checkbox.

If a container was created using a Docker run configuration, to view its deployment log, select it and open the Deploy log tab. To view the log messages from the container's STDOUT and STDERR, select it and open the Log tab. For more information, see the docker logs command reference.

You can browse the files inside a running container using the Files tab. Select any file and click to open it remotely in the editor or click to create a copy of the file as a scratch.

The file browser may not work by default for containers that don't have the full ls package, for example, images that are based on Alpine, Photon, and BusyBox. To use it, you can add the following command in the Dockerfile:

FROM photon:3.0 RUN echo y tdnf remove toybox
  1. In the Services tool window, right-click the container name and then click Exec.

  2. In the Run command in container popup, click Create.

  3. In the Exec dialog, type the command and click OK. For example:

    ls /tmp

    List the contents of the /tmp directory

    mkdir /tmp/my-new-dir

    Create the my-new-dir directory inside the /tmp directory

    /bin/bashStart a bash session

For more information, see the docker exec command reference.

View detailed information about a running container

  • In the Services tool window, right-click the container name and then click Inspect.

    The output is rendered as a JSON array on the Inspection tab.

For more information, see the docker inspect command reference.

  • In the Services tool window, right-click the container name and then click Show processes.

    The output is rendered as a JSON array on the Processes tab.

For more information, see the docker top command reference.

Attach a console to the output of an executable container

  • In the Services tool window, right-click the container and then click Attach.

    The console is attached to the output of the ENTRYPOINT process running inside a container, and is rendered on the Attached console tab.

For more information, see the docker attach command reference.

Docker Compose

Docker Compose is used to run multi-container applications. For example, you can run a web server, backend database, and your application code as separate services. Each service can be scaled by adding more containers if necessary. This enables you to perform efficient development and testing in a dynamic environment, similar to production.

Run a multi-container Docker application

  1. Define necessary services in one or several Docker Compose files.

  2. From the main menu, select Run Edit Configurations.

  3. Click , point to Docker and then click Docker-compose.

  4. Specify the Docker Compose files that define services which you want to run in containers. If necessary, you can restrict the services that this configuration will start, specify environment variables, and force building of images before starting corresponding containers (that is, add the --build option for the docker-compose up command).

  5. When the run configuration is ready, execute it.

To quickly create a Docker-compose run configuration and run it with default settings, right-click a Docker Compose file in the Project tool window and click Run in the context menu. You can also use gutter icons and the context menu in the Docker Compose file to control services.

When Docker Compose runs your multi-container application, you can use the Services tool window to control specific services and interact with containers. The containers are listed under the dedicated Compose nodes, not under the Containers node (which is only for standalone containers).

  1. In the Services tool window, select the service you want to scale and click or select Scale from the context menu.

  2. Specify how many containers you want for this service and click OK.

  • In the Services tool window, select the service and click or select Stop from the context menu.

Docker Install Postgresql Image

  • In the Services tool window, select the Compose node and click .

Docker Install Postgres 12

  • In the Services tool window, select the Compose node and click .

This stops and removes containers along with all related networks, volumes, and images.

Open the Docker Compose file that was used to run the application

Docker Install Postgres 10

  • In the Services tool window, right-click the Compose node or a nested service node and then click Jump to Source in the context menu F4.

The Docker-compose run configuration will identify environment files with the .env suffix if they are located in the same directory as the Docker Compose file.

Docker debug

To debug your application running in a Docker container, you can use the remote debug configuration:

  1. In the main menu, select Run Edit Configurations.

  2. Click and select Remote.

  3. In the Before launch section, click and select Launch Docker before debug.

  4. Specify the Docker configuration you want to run and configure the preferred container port for the debugger to attach to if the default one is allocated to something else.

  5. Check the Custom Command and modify it if necessary.

  6. Apply all changes, remove any running containers of the application you want to debug, and then launch the remote debug configuration.

For examples, see the following tutorials:

Troubleshooting

If you encounter one of the following problems, try the corresponding suggested solution.

Make sure that:

  • Docker is running.

  • Your Docker connection settings are correct.

If you are using Docker for Windows, enable the Expose daemon on tcp://localhost:2375 without TLS option in the General section of your Docker settings.

If you are using Docker Toolbox, make sure that Docker Machine is running and its executable is specified correctly in the Settings/Preferences dialog Ctrl+Alt+S under Build, Execution, Deployment Docker Tools.

Make sure that the Docker Compose executable is specified correctly in the Settings/Preferences dialog Ctrl+Alt+S under Build, Execution, Deployment Docker Tools.

Make sure that the corresponding container ports are exposed. Use the EXPOSE command in your Dockerfile.

Unable to associate existing Dockerfiles or Docker Compose files with relevant types

When you create new Dockerfiles or Docker compose files, IntelliJ IDEA automatically identifies their type. If a file type is not evident from its name, you will be prompted to select the file type manually. To associate an existing file with the correct type, right-click it in the Project view and select Associate with File Type from the context menu.

If the Associate with File Type actions is disabled, this probably means that the filename is registered as a pattern for current file type. For example, if you have a Dockerfile with a custom name that is recognized as a text file, you cannot associate it with the Dockerfile type. To remove the file type pattern, do the following:

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Editor File types.

  2. Select the relevant file type (in this case: Text) and remove the pattern with the name of the file.

  3. Click OK to apply the changes.

Docker Install Postgres Ubuntu

Now you should be able to set the correct file type using Associate with File Type in the context menu.

Limitations

Docker Install Postgres Windows

The Docker integration plugin has certain limitations and bugs, however JetBrains is constantly working on fixes and improvements for it. You can find the list of Docker issues in our bug tracking system and vote for the ones that affect you the most. You can also file your own bugs and feature requests.

What you will need to create a Docker Compose with Postgres

If you're not family with Docker yet, I recommend you to read my two previous articles. They will teach you all the necessary knowledge to understand this tutorial to run Postgres in Docker.

Why using Docker Compose to a create Postgres database

PostgreSQL is one of the most used database engines nowadays. If you're here today, it's because you probably need it in your project!

When I'm working on a new project, I like to have a clean environment. One of the things I dislike the most is installing 'big' tools on my computers (for example, a database engine).

I like to use containers for some of my tools. I'm not going to lie, I'm not constantly moving all my environments to Docker, but I like to have a part of it in a dedicated one.

One command is enough to set up a Postgres database in Docker from scratch with new parameters. With one command, you can also shut down all the environment and free your computer from work.

Create a Postgres Docker Compose

In this part, I will show you how to create a Postgres Docker Compose example. The configuration file will be detailed line per line, so you will be able to test it and configure it for your needs.

Step 1. Create the Docker Compose file

The first step consists of creating the configuration file to run Postgres in Docker. This file is called docker-compose.yml, and you can make it at your project's root.If you don't have any project yet, you can do it in a new folder on your computer.

Once you created the Docker Compose file, your folder architecture should be similar as below.

Step 2. Configure your Postgres Docker Compose file

To reach your goal of creating a Postgres Docker Compose file, we will use the image for PostgreSQL available on Docker Hub.This image will simplify the job by providing all the dependencies to run Postgres in Docker.

The main two steps of the configuration will be:

  1. import the Postgres image in the Docker Compose
  2. configure the database to fit your needs and use it on your computer

Below, you will find the Docker Compose file. Each line is commented on so that you can understand the purpose of each instruction.

Install Postgresql Docker Mac

With this first version of your Postgres Docker Compose file, you will be able to run the database and connect to it!

Step 2a. Optional Postgres Docker Compose configurations

Before running your file, I want to share with you some optional configurations. These steps are not mandatory, and to keep the Postgres Docker Compose example simple, we will not use them at the tutorial end.

Set-up an environment file for your Postgres database

If you don't want to put your environment variable in your Docker Compose file, you can create an environment file at your project root.This file should be named .env, and it should be located at the root of your repository with your Docker Compose.

As I explained in the Postgres Docker Compose comments, the environment variables are defined in the environment part of the configuration.If you copy/paste the environment variables from the Docker Compose file to your .env file, you will have the following:

Once you did it, it means your environment part isn't used anymore so that you can remove it.

If you want that your Postgres Docker Compose find the environment variables, you should add the following lines:

Your Docker Compose will use the environment variables defined inside the .env to configure the database.

Use a volume to persist data on your computer

By default, the Postgres data will be only in the container. If you decide to delete the container, all the data are going to disappear definitively.

To persist the data, we will use a Docker volume. It will share the database date inside of your container with a local folder of your computer.It means that everytime you will re-build your Postgres Docker Compose, these same data will be used for the current project.

To do that, you can add the database section in the services node of your Docker Compose.

Step 3. Run the Docker Compose

Your Docker Compose is ready! 🚀

In the root of your project repository, type the following command to install the dependencies and run your database:

Install Postgres In Centos Docker

That's it; everything is set up! To test your database, you should connect to it using the software or programming language of your choice.

As a reminder, the Postgres database is accessible on localhost with the port 5432. You can find the connection information (the username, the password, and the default database).

Step 4. Stop the Docker Compose

As I mentioned before, one of the advantages of using Docker Compose for Postgres is to avoid background processes on your computer.When you finish working on your project, I recommend you to stop the running Postgres Docker container using the command below:

Once you want to work again on your project, you can use docker-compose up again to run your database.

Postgresql Docker Volume

Code is available

If you want to read and use the complete Postgres Docker Compose example, I've hosted the code on my Github.Feel free to use it for your projects!

Using Postgresql Docker Image

➡️ If you want more content like this, you can follow me on Twitter, where I tweet about web development, self-improvement, and my journey as a fullstack developer!