# How to run PostgreSQL queries from GoLand

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/go/2026.3/llms.txt).

> **Note: Prerequisites**
> Before you begin, make sure the following software is installed:
>
>
>
> * Git 2.20.1 or later — [https://git-scm.com/](https://git-scm.com/)
>
> * Docker 18.09.0 or later — [https://www.docker.com/](https://www.docker.com/)
>
> * GoLand 2025.1 or later — [https://www.jetbrains.com/goland/download](https://www.jetbrains.com/goland/download)

## Starting a project

Procedure: Create a Go project

1. Select `File | New | Project…`.

Alternatively, navigate to New | Project on the Welcome to GoLand dialog.

2. In the New Project dialog, select Go from the list of available project types.

3. In the GOROOT field, specify the location of your Go installation. GoLand usually detects this location automatically.

To change or install a new Go SDK version, click Add SDK and choose one of the following options:

* Local: to use an existing SDK from your local system.

* Download: to download a Go SDK version from the official repository.

4. Click Create to create the project.

![Integration with Go](https://resources.jetbrains.com/help/img/idea/2026.3/go_general_procedure_of_creating_a_project.png)

Procedure: Create a Go file

1. To create a Go file, use one of the following options:

* In the Project tool window, right-click the parent folder of your project and select `New | Go FileGo File`.

* Click the parent folder of your project, press `Alt+Insert` (Windows), `⌘ N` (macOS), `⌃ N` (IntelliJ IDEA Classic (macOS)), `⌘ N` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌘ N` (Sublime Text (macOS)), `Alt+Insert` (VSCode), `⌃ ⏎` (VSCode (macOS)), and select Go File.

* Click the parent folder of your project, then go to `File | New | Go File`.

2. In the New Go File dialog, enter a file name and choose the file type:

* Empty file — creates an empty Go file.

* Simple application — creates a Go file with a predefined `main` function.

## Clone files from repositories

For this example, we will use the following two repositories: the Sakila dump files ([https://github.com/DataGrip/dumps](https://github.com/DataGrip/dumps)) and a Docker Compose file ([https://github.com/DataGrip/datagrip-documentation](https://github.com/DataGrip/datagrip-documentation)). The Sakila repository includes SQL scripts to create and populate the Sakila sample database. The Docker repository contains YAML files that define services, networks, and volumes.

Procedure: Clone repositories

1. Select `View | Tool Windows | Terminal` or press `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+`` (VSCode), `⌃ `` (VSCode (macOS)).

2. In the terminal, run the following commands:

```GIT
git clone https://github.com/DataGrip/dumps.git
```

```GIT
git clone https://github.com/DataGrip/datagrip-documentation.git
```

Two new folders for the cloned repositories will appear in the Project tool window.

![Clone repositories](https://resources.jetbrains.com/help/img/idea/2026.3/go_clone_repositories.png)

## Run a Docker container

In this tutorial, we will run a PostgreSQL container using Docker, based on the datagrip-documentation repository. Inside the repository's `Docker` directory, you will find the `.env` and `docker-compose.yml` files. The `.env` file contains credentials for the PostgreSQL service. The `docker-compose.yml` file defines how the service is created and configured.

The service name appears on the first line of the service definition, above the JDBC URL. In our example, the service name is postgresql_12, and the JDBC URL is: `jdbc:postgresql://localhost:54333/guest?user=guest&password=guest`

```YAML
postgresql_12:
  # jdbc:postgresql://localhost:54333/guest?user=guest&password=guest
  container_name: postgresql_12
  ports:
    - "54333:5432"
  image: postgres:12-alpine
  env_file: .env
  healthcheck:
    test: "exit 0"
```

To run the container from the command line, use the following command:

```
docker-compose up -d postgresql_12
```

Make sure to navigate to the directory that contains the `docker-compose.yml` file before running the command.

Alternatively, you can enable and use the Docker plugin in GoLand.

Procedure: Connect to the Docker daemon

1. Press `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) to open settings and then select `Build, Execution, Deployment | Docker`.

2. Click ![The Add button](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg) 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.

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

![The Docker connection settings](https://resources.jetbrains.com/help/img/idea/2026.3/go_DockerSettings.png)

For more information about mapping local paths to the virtual machine running the Docker daemon when using Docker on Windows or macOS, refer to [Virtual machine path mappings for Windows and macOS hosts](settings-docker.html#virtual-machine-path). You will not be able to use volumes and bind mounts for directories outside of the mapped local path.

This table is not available on a Linux host, where Docker runs natively and you can mount any directory to the container.

3. Open the Services tool window ( `View | Tool Windows | Services` or `Alt+8` (Windows), `⌘ 8` (macOS), `⌘ 8` (IntelliJ IDEA Classic (macOS)), `⌘ 8` (macOS System Shortcuts), `Alt+8` (XWin), `Alt+8` (GNOME), `Alt+8` (KDE), `Alt+8` (Emacs), `Alt+8` (Sublime Text), `⌘ 8` (Sublime Text (macOS)), `Alt+8` (VSCode), `⌘ 8` (VSCode (macOS)) ), select the configured Docker connection node ![Docker node](https://resources.jetbrains.com/help/img/idea/2026.3/clouds-docker-impl.icons.Docker.svg) and click ![The Connect button](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.execute.svg), or select Connect from the context menu.

![The Docker tool window, connected to Docker](https://resources.jetbrains.com/help/img/idea/2026.3/go_DockerConnected.png)

To edit the Docker connection settings, select the Docker node and click ![The Edit Configuration button](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.general.edit.svg) on the toolbar, or select Edit Configuration from the context menu.

You can also click ![the Add Service menu](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg) and select Docker Connection to add a Docker connection directly from the Services tool window. If you have [Docker contexts](https://docs.docker.com/engine/context/working-with-contexts/) configured, you can select Docker Connections from Docker Contexts to add the corresponding connections.

Procedure: Create the Docker Compose deployment configuration

1. In the Project tool window (`View | Tool Windows | Project`), locate the `docker-compose.yml` file from the [cloned repository](https://github.com/DataGrip/datagrip-documentation.git).

2. Click the Run icon in the gutter.

The `postgresql_12` container will start and appear in the Services tool window.

![Create the Docker Compose deployment configuration](https://resources.jetbrains.com/help/img/idea/2026.3/go_create_the_docker_compose_deployment_configuration.png)

> **Tip:**
> To view all running containers, use the `docker ps` command in the terminal. Alternatively, you can use the Docker tool window (`View | Tool Windows | Docker`) to view and manage Docker containers.

## Connect to a data source

Depending on the database vendor (MySQL, PostgreSQL, Oracle), you need to create a corresponding data source connection. In this tutorial, we will create a PostgreSQL connection. If you want to connect to a different database management system (DBMS), refer to [Create a data source](managing-data-sources.html#create_a_data_source).

Procedure:

1. Open data source properties by doing one of the following:

* On the Database tool window toolbar, click ![The Data Sources icon](https://resources.jetbrains.com/help/img/idea/2026.3/database-plugin.icons.expui.manageDataSources.svg) Data Sources.

* Press `Shift+Enter` (Windows), `⌘ I` (macOS), `⇧ ⏎` (IntelliJ IDEA Classic (macOS)), `⌘ I` (macOS System Shortcuts), `Shift+Enter` (XWin), `Shift+Enter` (GNOME), `Shift+Enter` (KDE), `Shift+Enter` (Emacs), `Shift+Enter` (Sublime Text), `⌘ I` (Sublime Text (macOS)), `Shift+Enter` (VSCode), `Shift+Enter` (VSCode (macOS)) .

![Open the Data Source and Drivers dialog](https://resources.jetbrains.com/help/img/idea/2026.3/open_data_sources_and_drivers_dialog.png)

2. In the Data Sources and Drivers dialog, click the Add icon (![The Add icon](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg)) and select PostgreSQL.

3. Check if there is a Download missing driver files link at the bottom of the connection settings area. Click this link to download drivers that are required to interact with a database. For a direct download link, refer to the [JetBrains JDBC drivers](https://www.jetbrains.com/datagrip/jdbc-drivers/) page.

![The Download missing driver files link](https://resources.jetbrains.com/help/img/idea/2026.3/db_connection_download_missing_drivers_link.png)

Location for the downloaded JDBC drivers is the [GoLand configuration directory](directories-used-by-the-ide-to-store-settings-caches-plugins-and-logs.html#config-directory).

You can also use your drivers for the database instead of the provided ones. For more information about connecting to a database with your driver, refer to [Add a user driver to an existing connection](jdbc-drivers.html#configure_a_jdbc_driver_for_an_existing_data_source).

If there is no Download missing driver files link, then you already have the required drivers.

4. In the URL field, paste the following JDBC URL:

```
jdbc:postgresql://localhost:54333/guest?user=guest&password=guest
```

You can find JDBC URLs for other DBMSs in the `docker-compose.yml` file. Open the file in a text editor to review available connection details.

5. (Optional) In the Name field, enter a custom name for the connection (for example, PostgreSQL).

6. Ensure that the database connection can be established using the provided details. To do this, click the Test Connection  link at the bottom of the connection details section.

If you encounter any connection issues, refer to the [Cannot connect to a database](connectivity-problems.html) page.

7. (Optional) By default, only the default database and schema are      introspected and available to work with. If you also want to work with other databases and schemas      , in the Schemas tab, select them for the introspection.

![Schemas tab of the Data Sources and Drivers dialog](https://resources.jetbrains.com/help/img/idea/2026.3/db_connection_schemas_tab.png)

8. Click OK to create the data source.

![Integration with PostgreSQL](https://resources.jetbrains.com/help/img/idea/2026.3/go_postgresql_integration.png)

> **Tip:**
> If the Connecting to <JDBC URL> dialog appears when you click the Test connection button, enter `guest` as the password and click OK.

## Run the dump files

Procedure: Create the database structure

1. In the Project (`View | Tool Windows | Project`) tool window, navigate to `dump/postgres-sakila-db/postgres-sakila-schema.sql`.

2. Right-click `postgres-sakila-schema.sql` and select Run postgres-sakila-schema.sql. Alternatively, open the file and press `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Shift+F10` (VSCode), `Ctrl+Shift+F10` (VSCode (macOS)).

3. In the Edit Configuration dialog, click the Add button in the Target data source/schema pane and select PostgreSQL.

4. Click Run to execute the script.

![Run schema dump file](https://resources.jetbrains.com/help/img/idea/2026.3/go_tutorial_run_dump_files.png)

Procedure: Load sample data into the database

1. In the Project (`View | Tool Windows | Project`) tool window, navigate to the dump/postgres-sakila-db directory.

2. Expand the postgres-sakila-db directory.

3. Right-click `postgres-sakila-insert-data.sql` and select Run postgres-sakila-insert-data.sql. Alternatively, open the file and press `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Shift+F10` (VSCode), `Ctrl+Shift+F10` (VSCode (macOS)).

4. In the Edit Configuration dialog, click the Add button and select PostgreSQL.

5. Click Run to execute the script.

![Insert data dump file run configuration](https://resources.jetbrains.com/help/img/idea/2026.3/db_tutorial_docker_database_data_run_dump_file_configuration.png)

## Fetch dependencies from the Go code

To issue a query to PostgreSQL, we are going to use the `sqlx` library, which provides a set of extensions for the Go standard `database/sql` package.  For more information about the library, refer to  For more details, visit [sqlx on GitHub](https://github.com/jmoiron/sqlx).

Procedure:

1. Open the created `main.go` file and paste the [following code](https://github.com/apronichev/documentation-code-examples/blob/master/connectToPostgreSQL/main.go) from GitHub into it.

2. Place the caret on the "github.com/jmoiron/sqlx" import statement, press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Ctrl+.` (VSCode), `⌘ .` (VSCode (macOS)), and select Fix missing dependencies (go mod tidy).

> **Tip:**
> Inject the correct SQL dialect to enable syntax highlighting and code completion in your query. For this example, use PostgreSQL. To apply the PostgreSQL dialect, press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Ctrl+.` (VSCode), `⌘ .` (VSCode (macOS)) and select Inject language or reference. In the list of languages, select PostgreSQL (SQL).

> **Tip:**
> To run only the SQL code, click the SQL section in the code, press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Ctrl+.` (VSCode), `⌘ .` (VSCode (macOS)), and select Run query in console.

## Run your code

Procedure:

* To run the configuration, click the Run icon (![The Run icon](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.execute.svg)) in the gutter near the `main` function and select Run 'go build main.go'.

![Run the configuration](https://resources.jetbrains.com/help/img/idea/2026.3/go_tutorials_run_configuration.png)

