How to run PostgreSQL queries from GoLand
note
PrerequisitesTo complete this tutorial, ensure that you have installed the following software:
Git 2.20.1 and later-https://git-scm.com/.
Docker 18.09.0 and later – https://www.docker.com/.
GoLand 2018.3 and later – https://www.jetbrains.com/goland/download.
For example purposes, we are going to use the following two repositories: Sakila dump files (https://github.com/DataGrip/dumps) and Docker-compose files (https://github.com/DataGrip/datagrip-documentation). Sakila repository includes scripts that generate the structure of the Sakila database and scripts that add data to database objects. Docker repository includes YAML files that define services, networks, and volumes.
Click VCS | Enable Version Control Integration in the menu.
In the Enable Version Control Integration dialog, ensure that
Git
is selected in the dropdown list and click OK.
Click Git | Clone.
In the URL field, copy and paste the following web URL:
Sakila dump files:
https://github.com/DataGrip/dumps.git
Docker-compose files:
https://github.com/DataGrip/datagrip-documentation.git
In the Directory field, click the folder icon (
) and specify a directory for the repository files.
For this tutorial, we are going to run a PostgreSQL container for Docker (from the datagrip-documentation repository). In the Docker directory of the repository, you can find .env and docker-compose.yml files. .env includes credentials for the PostgreSQL service. docker-compose.yml defines how the service is created and configured.
The service name is the first line of the service definition (before the JDBC URL). In our example, the service name is postgresql_12, the JDBC URL is jdbc:postgresql://localhost:54333/guest?user=guest&password=guest
.
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"
You can run the container from a command line by running:
docker-compose up -d postgresql_12
Note that you have to navigate to the directory of the docker-compose.yml file first.
Alternatively, you can enable and use the Docker plugin in GoLand.
Press CtrlAlt0S to open settings and then 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.
The Connection successful message should appear at the bottom of the dialog.
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. 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.
Open the Services tool window (View | Tool Windows | Services or Alt08), select the configured Docker connection 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.
You can also click
and select Docker Connection to add a Docker connection directly from the Services tool window. If you have Docker contexts configured, you can select Docker Connections from Docker Contexts to add the corresponding connections.
In the Services tool window (View | Tool Windows | Services), right-click the Docker node and select Deploy.
In the popup window, select Create Docker-compose Deployment.
In the Create Deployment Configuration dialog, click the Browse icon of the Compose file(s) field.
In the Docker Compose Configuration Files dialog, click the Add button and navigate to the compose file. The Compose YAML file defines services, networks, and volumes. In our case, it is docker-compose.yml
In the Docker Compose Configuration Files dialog, click OK.
In the Service(s) field, type
postgresql_12
.Click Run.
As a result, you can see the
postgresql_12
running container in the Services tool window.Gif
tip
To view all running images, use the
docker ps
command in the command line. Otherwise, use the Docker tool window (View | Tool Windows | Docker) to view and manage Docker containers.
Depending on a 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 other database management systems (DBMS), refer to Create a data source.
Open data source properties. You can open data source properties by using one of the following options:
In the Database tool window (View | Tool Windows | Database) , click the Data Source Properties icon
.
Press CtrlAltShift0S.
In the Data Sources and Drivers dialog, click the Add icon (
) and select PostgreSQL.
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 page.
Location for the downloaded JDBC drivers is the GoLand configuration 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.
If there is no Download missing driver files link, then you already have the required drivers.
In the URL field, copy and paste the following JDBC URL:
jdbc:postgresql://localhost:54333/guest?user=guest&password=guest
Find the JDBC URL for other DBMS in docker-compose.yml. You can open docker-compose.yml in a text editor.
(Optional) In the Name field, delete the current name and type a new name for the connection (for example, PostgreSQL).
Ensure that the connection to the database can be established using the provided details. To do that, click the Test Connection link at the bottom of the connection details area.
In case of any connection issues, refer to the Cannot connect to a database page.
(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.
Click OK to create the data source.
![Integration with PostgreSQL Integration with PostgreSQL](https://resources.jetbrains.com/help/img/idea/2024.3/go_postgresql_integration.png)
tip
If you see the Connecting to <JDBC URL> dialog when you click the Test connection button, specify
guest
as a password and click OK.
To run dump files from the project in GoLand, attach a directory with dump files.
Click File | Open.
Navigate to the folder that you want to attach. In this tutorial, this folder is dumps that you cloned on Step 1 (Sakila dump files).
Click Open.
In the Open Project dialog, click Attach.
![Attach a directory to the project Attach a directory to the project](https://resources.jetbrains.com/help/img/idea/2024.3/go_tutorial_attach_directory.png)
In the Project (View | Tool Windows | Project) tool window, navigate to the postgres-sakila-db tree node.
Expand postgres-sakila-db tree node.
Right-click the postgres-sakila-schema.sql and select Run postgres-sakila-schema .sql. Alternatively, click the postgres-sakila-schema.sql file and press CtrlShiftF10.
In the Edit Configuration dialog, click the Add button and select PostgreSQL.
Press Run.
Gif
In the Project (View | Tool Windows | Project) tool window, navigate to the postgres-sakila-db tree node.
Expand postgres-sakila-db tree node.
Right-click the postgres-sakila-insert-data.sql and select Run postgres-sakila-insert-data.sql. Alternatively, click the postgres-sakila-insert-data.sql file and press CtrlShiftF10.
In the Edit Configuration dialog, click the Add button and select PostgreSQL.
Press Run.
To issue a query to PostgreSQL, we are going to use the sqlx
library that provides a set of extensions for the Go standard database/sql
library. For more information about the library, refer to sqlx on GitHub.
main.go at GitHub: click the link to get the code from the GiHub repository.
Create a Go project in GoLand.
Right-click the root folder in the project and navigate to New | Go File.
In the New Go File dialog, type
main
in the Name field and click OK.Copy the attached code snippet and paste it to the main.go file.
Place the caret at the "github.com/jmoiron/sqlx" import line, press AltEnter and click go get -t github.com/jmoiron/sqlx.
tip
Inject a correct SQL dialect to have a correct syntax highlighting and code completion in your query. For our example, it is PostgreSQL. To apply the PostgreSQL dialect, press AltEnter and select Inject language or reference. In the list of languages, select PostgreSQL (SQL).
tip
To run just the SQL code, click the SQL part in the code, press AltEnter, and select Run query in console.
To run the configuration, click the Run icon (
) in the gutter near the main function and select Run 'go build main.go'.
Thanks for your feedback!