A lightweight Neovim plugin to work with PostgreSQL directly from your editor. Forget external connection managers—keep your workflow entirely within Neovim.
- No External Dependencies: Manages connections internally. The only requirement is having the
psqlcommand-line tool installed. - Simple Configuration: Configure all your connections with a simple Lua table.
- Interactive Shell: Quickly launch a
psqlshell for any configured database in a Neovim terminal. - Run Queries: Execute SQL from your buffer and see the results in a new split.
- Neovim >= 0.8
psqlcommand-line utility available in your system'sPATH.
Install using your favorite plugin manager.
-- lua/plugins/psql.lua
return {
'axonde/psql.nvim',
cmd = { 'Psql', 'PsqlExec', 'PsqlListDBs' },
config = function()
require('psql').setup({
-- Your configuration here
})
end,
}But this is not a perfect way, if you are sharing the config to a git instance, where people can get easily your credit information. To fix this (cause I have this problem, uuff it was an open source database credit) you can create a global psql.json file and store all the info in it.
And than create a config like this:
local function loadJsonFromFile(filepath)
local file = io.open(filepath, "r")
if not file then
vim.notify("Cannot open the file: " .. filepath, vim.log.levels.ERROR)
return nil
end
local content = file:read("*a")
io.close(file)
local ok, decoded_json = pcall(vim.fn.json_decode, content)
if not ok then
vim.notify("Ill formated JSON: " .. filepath, vim.log.levels.ERROR)
return nil
end
return decoded_json
end
local connections = loadJsonFromFile("your-path-to-the-secret-file.json")
return {
"axonde/psql.nvim",
cmd = { "Psql", "PsqlExec", "PsqlListDBs" },
config = function()
require("psql").setup({
connections = connections,
runner_output = "split",
})
end,
}Here is a full example of the setup function.
-- In your config function:
require('psql').setup({
-- A list of your database connections
connections = {
{
name = "My Local DB",
-- You can use a standard PostgreSQL URL
url = "postgresql://user:password@localhost:5432/mydatabase",
},
{
name = "Work Staging (no password)",
-- Or define connection parameters individually
host = "staging.example.com",
port = 5432,
user = "readonly_user",
dbname = "staging_db",
},
},
-- Determines how query results are displayed.
-- Can be 'split', 'vsplit', or 'term'.
runner_output = 'split',
})connections(table): A list of your database connections. Each connection is a table that must have anameand either:- A
urlstring. - Individual properties:
host,port,user,password,dbname.
- A
runner_output(string): Defines where the output of non-interactive queries is shown.'split': In a new horizontal split (default).'vsplit': In a new vertical split.'term': In a new terminal buffer.
The plugin provides three main commands:
-
:PsqlOpens a selector (vim.ui.select) to choose a configured database. It then opens an interactivepsqlshell for that connection in a new terminal window. -
:PsqlExecExecutes the entire content of the current buffer as a SQL query. It will first ask you to select a connection. The results are displayed based on yourrunner_outputsetting. -
:PsqlListDBsA shortcut to list all databases on a selected server (runs the\lcommand inpsql).
Contributions, issues, and feature requests are welcome. Feel free to check the issues page.
This project is licensed under the MIT License.