IntelliJ IDEA 2020.2 Help

Run a database in a Docker container

You can use Docker to run a database container and test how your application interacts with it. This tutorial describes how to run a Docker container with a PostgeSQL 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 Docker tool window, right-click the Images node, and then click Pull image.

  2. In the Pull Image dialog, select the default Docker Hub registry and specify the PostgreSQL server image repository name and tag postgres and latest.

  3. Click OK and wait until the image is pulled.

docker postgres pull png

Run a container from the PostgreSQL server image

  1. In the Docker tool window, right-click the PostgreSQL server image and then click Create container.

  2. In the Create container popup, click Create.

  3. In the Create Docker Configuration dialog, specify the name of the container, select All to publish all exposed container ports to the host interfaces, and click Run.

docker postgres run png

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 Docker tool window, click Inspect and then find the value of the HostPort field in the output.

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

  2. Click New icons.general.add.svg, point to Data Source, and then click PostgreSQL.

  3. Set the host name to localhost, port to 32768 (or whatever host port it was mapped to), default database and user to postgres. No password is required by default.

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

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

docker postgres connect png

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

Last modified: 19 August 2020