Simple utility to execute SQL queries from within Neovim. The result of the query will be displayed in a horizontal split in Neovim.
- Postgresql
- Mysql
<leader>q
(Normal mode) Execute whole file content<leader>q
(Visual mode) Execute selected content<leader>l
(Normal mode). Execute current line
If the installation is successful and the host is correctly specified in the sql_hosts.json
file, type :SqlRun
from
the buffer where the query is written. Follow the instructions to select the server and the database
(if not specified in the host configuration) against which the query needs to be executed.
At the end, the keybindings to execute the query will be available for the buffer.
{
'mrvaita/sqlrun.nvim',
opts = {},
},
The only configuration parameter available is the path to a json file where the database connection parameters are specified.
{
'mrvaita/sqlrun.nvim',
opts = { hosts_path = "~/.config/sqlrun.nvim/sql_hosts.json" },
}
The default file name is ~/.config/sqlrun.nvim/sql_hosts.json
. Each hosts specified in the configuration file
should have the following fields:
{
"connection_name": {
"server": "servername",
"port": 5432,
"binary": "/usr/bin/psql"
"user": "username",
"password": "",
"database": "",
"ssh": false,
"db_type": "postgresql"
}
}
If the server can be reached by the database client it is enough to specify the desired host in the server
field in
the configuration file. ssh
option must be set to false
or omitted.
Remote servers can be specified to execute queries via ssh client. For the plugin to work few conditions must be met.
- The current user (not the database username) can ssh into the server
- The current user did ssh into the server at least once and added the server to list of known hosts (typing
yes
in the terminal) - The current user added the RSA or DSA identities to the authentication agent (eg
$ ssh-add ~/.ssh/id_rsa
and entered the password). That way the password will not be requested the next time an ssh connection is performed. - Set the
ssh
option totrue
.
If necessary, it is possible to open an ssh tunnel via jump host to reach the database server. This is equivalent to the
ssh command ssh -L local_port:remote.server.net:database_port jump.host.net
. This is achieved with a go script included
in the plugin. I decided to include this functionality just to avoid to open a new terminal and execute the ssh command.
- The go program is built everytime neovim is started. Doesn't take long though
- Go >= 1.21 must be installed and present in the path
- On neovim start the go program is built in the directory where the plugin is installed
- Ssh logs are saved to a file under
~/.config/sqlrun.nvim/ssh_tunnel
- Only the current user is supported to connect to the jump host
- The jump host must be present in the list of
known_hosts
- Authentication is performed exclusively with ssh agent
- Plugin options should at least look like
opts = { ssh_tunnel = true }
- The database host in the json file should be specified like
{
"connection_name": {
"server": "servername",
"port": 51015,
"binary": "/usr/bin/psql"
"user": "username",
"password": "",
"database": "",
"db_type": "postgresql"
"ssh_tunnel": {
"jump_host": "jump.host.net:ssh_port",
"remote_host": "remote.host.net:remote_port"
}
}
}
- If the jump host
ssh_port
is not specified the port 22 will be used - The
port
option in this case refers to the local port where the traffic from the remote host is forwarded. The database port should be specified underremote_host
ssh
option must be set to false or omitted.
SqlRun can use lualine.nvim to display connection information in the following way
local sqlrun = require('sqlrun')
require('lualine').setup{
sections = {
lualine_x = {
{ sqlrun.get_current_connection_info, cond = sqlrun.is_connection_available },
},
},
}
The connection info will be displayed as <connection-name>:<database-name>