IntelliJ IDEA 2020.3 Help

Run a database in a Docker container

You can use Docker to run a database in a container as if it were a remote server, and test how your application interacts with it. This tutorial describes how to run a Docker container with a PostgreSQL server and connect to it using IntelliJ IDEA.

Before you begin, make sure that Docker integration is properly configured.

Pull a PostgreSQL server image

  1. In the Services tool window, expand your Docker connection and select the Images node.

  2. In the Image to pull field, start typing postgres and select the necessary image repository. For example, select postgres to pull the default postgres:latest image.

  3. Press Ctrl+Enter and wait until Docker pulls the image.

Run a container from the PostgreSQL server image

  1. Expand the Images node, select the PostgreSQL server image, and click The Create Container button. Alternatively, you can right-click the image and select Create Container.

  2. In the Create Docker Configuration dialog, specify the name of the container and select All to publish all exposed container ports to the host interfaces. For a more secure setup, you will need to define specific port bindings, of course. However, in this tutorial, we assume that your local machine is secure enough and there is nothing important.

  3. Configure environment variables that define the authentication settings for your PostgreSQL server. For the purposes of this tutorial, we can use Trust Authentication. Keep in mind, that this will enable anyone who can connect to the PostgreSQL server to access the database with any username of their choice.

    In the Environment variables field, click The Browse button and add the POSTGRES_HOST_AUTH_METHOD variable with the value trust.

  4. Check the Command preview field. The command should be similar to the following:

    docker run \ -P \ --env POSTGRES_HOST_AUTH_METHOD=trust \ postgres:latest

    Click Run.

Connect to the PostgreSQL server

Docker automatically maps the default PostgreSQL server port 5432 in the container to a host port within the ephemeral port range (typically from 32768 to 61000). This tutorial assumes that port 32768 was used. To check what the actual mapping is, right-click the PostgreSQL container in the Services tool window under the Containers node, click Inspect, and then find the value of the HostPort field in the Inspection output.

  1. Open the Database tool window (View | Tool Windows | Database ).

  2. Click The New button and select Data Source | PostgreSQL.

  3. Set the host name to localhost, the port number to 32768 (or whatever host port it was mapped to), the default database and user to postgres. No password is required since we used trust authentication to run the PostgreSQL server.

  4. If necessary, download the driver and test the connection.

  5. Click OK to add the PostgreSQL server as a data source.

For more information about working with database sources in IntelliJ IDEA, see Database tools and SQL.

Last modified: 08 March 2021