# Run a database in a Docker container

> **TL;DR**
> Required plugin: Docker
>
>
>
> Required software: Docker Desktop

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.

In this tutorial, you will learn how to run a Docker container with a PostgreSQL server and connect to it using IntelliJ IDEA.

> **Note:**
> Before you begin, make sure to [install and run
> Docker](docker.html#install_docker), [enable the Docker plugin](docker.html#enable_docker_plugin), and [connect to the
> Docker daemon in
> IntelliJ IDEA](docker.html#connect_to_docker).

Procedure: 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.

![Pull the Docker image with PostgreSQL](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgres_pull.png)

3. Press `Ctrl+Enter` (Windows), `⌘ ⏎` (macOS), `⌘ ⏎` (IntelliJ IDEA Classic (macOS)), `⌘ ⏎` (macOS System Shortcuts), `Ctrl+Enter` (XWin), `Ctrl+Enter` (GNOME), `Ctrl+Enter` (KDE), `Ctrl+Enter` (Emacs), `Ctrl+Enter` (Sublime Text), `⌘ ⏎` (Sublime Text (macOS)), `Ctrl+Enter` (NetBeans), `Ctrl+Enter` (Visual Studio), `⌘ ⏎` (Visual Studio (macOS)), `Ctrl+Enter` (Eclipse), `⌘ ⏎` (Eclipse (macOS)) and wait until Docker pulls the image.

![The Docker image was pulled](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgres_pulled.png)

Procedure: Run a container from the PostgreSQL server image

1. Expand the Images node, select the PostgreSQL server image, and click ![The Create Container button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) Create Container.

![Select the PostgreSQL image and click Create Container](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgres_create_container.png)

2. In the Create Docker Configuration dialog, click Modify options and select Randomly publish all exposed ports to publish all exposed container ports to the host interfaces.

> **Tip:**
> This tutorial assumes that your local machine is sufficiently secure and does not contain sensitive data. However, for a more secure configuration, it is important to define specific port bindings. The use of randomly published ports is intended for development and testing purposes only and should not be used in production environments.

3. Click Modify options and select Environment variables. In the Environment variables field that appears, you can configure the environment variables for your PostgreSQL authentication.

![Docker postgresql modify options](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgresql_modify_options.png)

4. Click ![The Browse button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.menu-open.svg)  Browse. In the Environment Variables dialog that opens, click ![The Add button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.general.add.svg) Add and add the `POSTGRES_HOST_AUTH_METHOD` variable with the value `trust`.

![The Environment Variables dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgresql_environment_variables.png)

> **Note:**
> For the purposes of this tutorial, we use [Trust Authentication](https://www.postgresql.org/docs/current/auth-trust.html). 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.
>
>
>
> For more information about authenticating, see the [official PostgreSQL Docker Hub image page](https://hub.docker.com/_/postgres).

5. In the Name and Container name fields, specify custom names for the configuration (for example, `PostgreSQL Container`) and container (for example, `postgresql`).

![The Create Docker Configuration dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgresql_create_configuration.png)

6. Apply the changes and click Run.

IntelliJ IDEA creates and starts a new Docker container. It will appear in the Services tool window under the Containers node.

![Running the created Docker container](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgresql_running_container.png)

Procedure: 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). In this tutorial, we use port 55000 as an example.

> **Tip:**
> To check what the actual mapping is, right-click the PostgreSQL container in the Services tool window under the Containers node, select Inspect, and then find the value of the `HostPort` field in the Inspection output.
>
> ![Port mapping check in the Services tool window](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_postgresql_container_hostport.png)

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

2. Click ![The New button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) New and select `Data Source | PostgreSQL`.

3. Set the hostname to `localhost`, the port number to `55000` (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](jdbc-drivers.html) and test the connection.

![The Data Sources and Drivers dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.2/docker_new_database.png)

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

The created data source will appear in the   Database   tool window . Now you can expand the connection tree to view the database objects of your PostgreSQL server running in Docker. For more information about the PostgreSQL database connection process and available options, refer to [Connect to a PostgreSQL database](postgresql.html#connect-to-postgresql-database).

## Summary

In this tutorial, you have learned how to:

* Pull and manage Docker images

* Configure and run a container from a DBMS (like PostgreSQL) image

* Establish a connection between IntelliJ IDEA and a database running inside a container

## See also

### Procedures

[Docker images](docker-images.html) [Docker containers](docker-containers.html)

### Reference

[Database Tools and SQL](relational-databases.html)

