---
title: "A Database Tool in Neovim with vim-dadbod"
canonical: "https://mnafshin.info/devtools/11-database-tool-dadbod/"
author: "Afshin Amini"
date: 2026-11-29
---
# A Database Tool in Neovim with vim-dadbod

While you build a Spring Boot app you often want to look at the database behind it. IntelliJ IDEA Ultimate has a Database tool window for that. In Neovim the same job is done by vim-dadbod, a small plugin that sends SQL to a database client and shows the result in a buffer, and two companions that add a browser tree and completion. This part covers only the editor tool; the SQL and database ideas themselves are covered in the [database section](/db/).

## The three plugins

- **vim-dadbod** runs a query against a database URL and shows the result in a buffer.
- **vim-dadbod-ui** adds a sidebar with connections, schemas, tables and ready-made queries.
- **vim-dadbod-completion** completes table names in SQL buffers.

LazyVim installs all three with the `lang.sql` extra in `:LazyExtras`. dadbod calls the database's own command-line client, so for PostgreSQL you need `psql` installed.

## Which databases work

dadbod has adapters for PostgreSQL, MySQL and MariaDB, SQLite, SQL Server, Oracle, ClickHouse, DuckDB and several others. It has **no adapter for H2**, the database module 9 of the companion code uses by default, and no generic JDBC support. The last sections explain where H2 keeps its data and the one path to it that worked.

For the main example we ran module 9 against a local PostgreSQL 17 database named `shop`, by overriding the `spring.datasource` properties on the command line. The app's seed code filled the tables.

## Run a query

The quickest way is the `:DB` command with a connection URL and a query:

```vim
:DB postgresql://shop:shop@localhost:5432/shop select p.name, c.name as category from products p join categories c on c.id = p.category_id order by p.id
```

A split opens with the result, formatted as a table by `psql`:

```text
      name      | category
----------------+----------
 Coffee         | Drinks
 Tea            | Drinks
 Croissant      | Bakery
 Espresso beans | Beans
(4 rows)
```

[ShopApp.java](https://github.com/mnafshin/springuniverse-code/blob/main/module-09-catalog/src/main/java/info/mnafshin/springuniverse/shop/ShopApp.java)

## Save the connection

Define named connections once, for example in `lua/config/options.lua`:

```lua
vim.g.dbs = { shop = "postgresql://shop:shop@localhost:5432/shop" }
```

That puts a password in a file; for real databases use the `DBUI_URL` environment variable or `:DBUIAddConnection` instead.

## Browse with the sidebar

Press `<leader>D` to toggle the sidebar and Enter on `shop` to connect. Under Schemas, `public` holds the two tables.

Open `products` and you get helpers: List, Columns, Primary Keys, Indexes, References and Foreign Keys. Each is a prepared query that runs as soon as you open it.

"New query" opens an SQL buffer bound to the connection. Write a query and run it with `<leader>S`, vim-dadbod-ui's key for executing the buffer.

## Completion in SQL buffers

In a query buffer, typing `prod` offers `products` from the live database. Column suggestions were SQL keywords, not columns.

## Where H2 keeps its data

H2 can live in three places, and the place decides who can read it:

- **In memory, inside the app's JVM.** This is module 9's default. It disappears when the app stops, and no other process can reach it.
- **In a file**, such as `jdbc:h2:file:./data/shop`. The data survives a restart, but the running app locks the file ("Database may be already in use"). After it stops, other tools can open it.
- **Behind an H2 server**, such as `jdbc:h2:tcp://localhost:9092/shop`. A separate H2 process owns the database, in memory or in a file, and several programs can connect.

None of these makes dadbod work on its own: `:DB jdbc:h2:...` fails with "DB: no adapter for h2".

## Reaching H2 through its PostgreSQL mode

The H2 server can also speak PostgreSQL's protocol, which its docs call experimental, so dadbod's PostgreSQL adapter can use it. Start a server with a folder for its files and point the app at it:

```bash
java -cp h2-2.4.240.jar org.h2.tools.Server -tcp -pg -pgPort 5435 -ifNotExists -baseDir /tmp/h2srv
```

```text
--spring.datasource.url=jdbc:h2:tcp://localhost:9092/shop;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
--spring.jpa.hibernate.ddl-auto=update
```

`psql` cannot log in with an empty password, so we created an H2 user (`CREATE USER EDITOR PASSWORD 'editor' ADMIN`). Then `:DB postgresql://editor:editor@localhost:5435/shop` returned the same four rows while the app ran, and still after it stopped. Set `vim.g.db_ui_use_postgres_views = 0` so the sidebar skips a view H2 lacks.

Those mode settings are required. H2's PostgreSQL server always uses them, so a file created without them failed with "Schema PUBLIC not found". `AUTO_SERVER=TRUE` let Java tools read the file while the app ran, but not this server.

Tables, List, Columns, Primary Keys and table completion worked; Foreign Keys showed an extra wrong row, and Indexes failed. Outside dadbod, Harlequin with its H2 adapter also queried the file from a Neovim `:terminal`.

## Compared with IntelliJ IDEA

IntelliJ's Database tool window adds an editable result grid, table diagrams, column completion and links from JPA entities to tables. For H2, the three placements behave the same way as here. An in-memory database inside a running app is still out of reach, and a file the app has open is still locked, unless the URL uses `AUTO_SERVER=TRUE` or the app uses an H2 TCP server. IntelliJ talks to H2 through JDBC, so it does not need the PostgreSQL-mode workaround that dadbod needs. dadbod keeps the query and the result as text you can search and yank.

## Recap

- `lang.sql` installs vim-dadbod, vim-dadbod-ui and vim-dadbod-completion; dadbod uses the database's own client, such as `psql`.
- `:DB <url> <query>` runs SQL and shows the result in a buffer; `vim.g.dbs` saves named connections.
- `<leader>D` opens the sidebar with schemas, tables and helper queries; `<leader>S` runs the query buffer.
- Table names complete from the live database; column completion was not reliable in our test.
- H2 in memory, in a file or behind a server: dadbod reaches it only through H2's PostgreSQL server, with the database in `MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE`; some helpers fail.
- IntelliJ has the same H2 placement limits. It uses JDBC, so it does not need the PostgreSQL-mode workaround, and it adds a result grid, diagrams and entity links.

**Series: Neovim as a Java IDE.** Previous: [Java Flight Recorder from Neovim](/devtools/10-java-flight-recorder/) · Next: [Dockerfiles, Compose files and Kubernetes YAML in Neovim](/devtools/12-docker-and-kubernetes-files/)

## Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.

- [vim-dadbod README](https://github.com/tpope/vim-dadbod)
- [vim-dadbod-ui README](https://github.com/kristijanhusak/vim-dadbod-ui)
- [vim-dadbod-completion README](https://github.com/kristijanhusak/vim-dadbod-completion)
- [LazyVim: lang.sql extra](https://www.lazyvim.org/extras/lang/sql)
- [IntelliJ IDEA: Database tool window](https://www.jetbrains.com/help/idea/database-tool-window.html)
- [IntelliJ IDEA: H2](https://www.jetbrains.com/help/idea/h2.html)
- [Spring Boot reference: SQL databases](https://docs.spring.io/spring-boot/reference/data/sql.html)
- [H2: ODBC driver and the PostgreSQL server mode](https://h2database.com/html/advanced.html#odbc_driver)
- [H2: compatibility modes](https://h2database.com/html/features.html#compatibility)
- [H2: connection modes (embedded, server, mixed) and database URLs](https://h2database.com/html/features.html#connection_modes)
- [harlequin-h2 README](https://github.com/clang-engineer/harlequin-h2)
- [Simon Späti: Query Databases in Neovim (DBUI)](https://www.ssp.sh/brain/query-databases-in-vim-vim-dadbod/)
- [Oliver Davies: Managing databases with Neovim and Docker](https://www.oliverdavies.uk/blog/managing-databases-neovim-docker)
- [Js' Technical Blog: vim-dadbod-ui connection via SSH tunnel](https://jslim.net/blog/2022/06/17/Vim-DB-client-vim-dadbod-ui-connection-via-SSH-tunnel/)
