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 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.
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.
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:
: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:
name | category
----------------+----------
Coffee | Drinks
Tea | Drinks
Croissant | Bakery
Espresso beans | Beans
(4 rows)
Save the connection
Define named connections once, for example in lua/config/options.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:
java -cp h2-2.4.240.jar org.h2.tools.Server -tcp -pg -pgPort 5435 -ifNotExists -baseDir /tmp/h2srv
--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.sqlinstalls vim-dadbod, vim-dadbod-ui and vim-dadbod-completion; dadbod uses the database’s own client, such aspsql.:DB <url> <query>runs SQL and shows the result in a buffer;vim.g.dbssaves named connections.<leader>Dopens the sidebar with schemas, tables and helper queries;<leader>Sruns 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 · Next: Dockerfiles, Compose files and Kubernetes YAML in Neovim
Sources
Official documentation and well-known tutorials read while writing this article. The wording here is our own.
- vim-dadbod README
- vim-dadbod-ui README
- vim-dadbod-completion README
- LazyVim: lang.sql extra
- IntelliJ IDEA: Database tool window
- IntelliJ IDEA: H2
- Spring Boot reference: SQL databases
- H2: ODBC driver and the PostgreSQL server mode
- H2: compatibility modes
- H2: connection modes (embedded, server, mixed) and database URLs
- harlequin-h2 README
- Simon Späti: Query Databases in Neovim (DBUI)
- Oliver Davies: Managing databases with Neovim and Docker
- Js’ Technical Blog: vim-dadbod-ui connection via SSH tunnel
Comments