lazysql
                                
                                
                                
                                    lazysql copied to clipboard
                            
                            
                            
                        A cross-platform TUI database management tool written in Go.
LAZYSQL
A cross-platform TUI database management tool written in Go.
Table of Contents
- 
      About The Project
      
- Built With
 
 - Features
 - 
      Getting Started
      
- Installation
 
 - Usage
 - Commands
 - Keybindings
 - Roadmap
 - Contributing
 - License
 - Contact
 - Acknowledgments
 
About The Project

This project is heavily inspired by Lazygit, which I think is the best TUI client for Git.
I wanted to have a tool like that, but for SQL. I didn't find one that fits my needs, so I created one myself.
I live in the terminal, so if you are like me, this tool can become handy for you too.
This is my first Open Source project, also, this is my first Go project. I am not a brilliant programmer. I am just a typical JavaScript developer that wanted to learn a new language, I also wanted a TUI SQL Client, so blanca y en botella, leche! (white and bottled).
This project is in ALPHA stage, please feel free to complain about my spaghetti code.
I use Lazysql daily in my full-time job as a full-stack javascript developer in its current (buggy xD) state. So, the plan is to improve and fix my little boy as a side-project in my free time.
Built With
Features
- [x] Cross-platform (macOS, Windows, Linux)
 - [x] Vim Keybindings
 - [x] Can manage multiple connections (Backspace)
 - [x] Tabs
 - [x] SQL Editor (CTRL + e)
 
Getting Started
Installation
Homebrew
brew tap jorgerojas26/lazysql
brew install lazysql
Install with go package manager
go install github.com/jorgerojas26/lazysql@latest
Binary Releases
For Windows, macOS or Linux, you can download a binary release here
Third party (maintained by the community)
Arch Linux users can install it from the AUR with:
paru -S lazysql
or
yay -S lazysql
or install it manual with:
git clone https://aur.archlinux.org/lazysql.git
cd lazysql
makepkg -si
(back to top)
Configuration
If the XDG_CONFIG_HOME environment variable is set, the configuration file will be located at:
${XDG_CONFIG_HOME}/lazysql/config.toml
If not, the configuration file will be located at:
- Windows: 
%APPDATA%\lazysql\config.toml - macOS: 
~/Library/Application Support/lazysql/config.toml - Linux: 
~/.config/lazysql/config.toml 
The configuration file is a TOML file and can be used to define multiple connections.
Example configuration
[[database]]
Name = 'Production database'
Provider = 'postgres'
DBName = 'foo'
URL = 'postgres://postgres:urlencodedpassword@localhost:${port}/foo'
Commands = [
  { Command = 'ssh -tt remote-bastion -L ${port}:localhost:5432', WaitForPort = '${port}' }
]
[[database]]
Name = 'Development database'
Provider = 'postgres'
DBName = 'foo'
URL = 'postgres://postgres:urlencodedpassword@localhost:5432/foo'
[application]
DefaultPageSize = 300
DisableSidebar = false
SidebarOverlay = false
The [aplication] section is used to define some app settings. Not all settings are available yet, this is a work in progress.
Usage
For a list of keyboard shortcuts press
?
Open the TUI with:
$ lazysql
To launch lazysql with the ability to pick from the saved connections.
$ lazysql [connection_url]
To launch lazysql and connect to database at [connection_url].
Connect to a DB
- Start 
lazysql - Create a new connection (press 
n) - Provide a name for the connection as well as the URL to connect to (see example connection URL)
 - Connect to the DB (press 
<Enter>) 
If you already have a connection set up:
- Start 
lazysql - Select the right connection (press 
jandhfor navigation) - Connect to the DB (press 
cor<Enter>) 
Create a table
There is currently no way to create a table from the TUI. However you can run the query to create the table as a SQL-Query, inside the SQL Editor.
You can update the tree by pressing R, so you can see your newly created table.
Execute SQL querys
- Press 
<Ctrl+E>to open the built-in SQL Editor - Write the SQL query
 - Press 
<Ctrl+R>to execute the SQL query 
To switch back to the table-tree press
HAfter executing a
SELECT-query a table will be displayed under the SQL-Editor with the query-result.
To switch focus back to SQL-Editor press/
Open/view a table
- Expand the table-tree by pressing 
eor<Enter> - Select the table you want to view
- next node 
j - previous node 
k - last node 
G - first node 
g 
 - next node 
 - Press 
<Enter>to open the table 
To switch back to the table-tree press
H
To switch back to the table pressL
Filter rows
- Open a table
 - Press 
/to focus the filter input - Write a 
WHERE-clause to filter the table - Press 
<Enter>to submit your filter 
To remove the filter, focuse the filter input (press
/) and press<Esc>.
Insert a row
- Open a table
 - Press 
1to switch to the record tab - Press 
oto insert a new row - Fill out all columns
 - Press 
<Ctrl+S>to save the changes 
Edit a column
- Open a table
 - Press 
1to switch to the record tab - Move to the column you want to edit
 - Press 
cto edit, Press<Enter>to submit - Press 
<Ctrl+S>to save the changes 
(back to top)
Support
- [x] MySQL
 - [x] PostgreSQL
 - [x] SQLite
 - [x] MSSQL
 - [ ] MongoDB
 
Support for multiple RDBMS is a work in progress.
Commands
In some cases, mostly when connecting to remote databases, it might be necessary to run a custom command before being able to connect to the database. For example when you can only access the database through a remote bastion, you would probably first need to open an SSH tunnel by running the following command in a separate terminal:
ssh remote-bastion -L 5432:localhost:5432
In order to make it easier to run these commands, lazysql supports running custom commands before connecting to the database. You can define these commands in the configuration file like this:
[[database]]
Name = 'server'
Provider = 'postgres'
DBName = 'foo'
URL = 'postgres://postgres:password@localhost:${port}/foo'
Commands = [
  { Command = 'ssh -tt remote-bastion -L ${port}:localhost:5432', WaitForPort = '${port}' }
]
The Command field is required and can contain any command that you would normally run in your terminal.
The WaitForPort field is optional and can be used to wait for a specific port to be open before continuing.
When you define the ${port} variable in the URL field, lazysql will automatically replace it with a random
free port number. This port number will then be used in the connection URL and is available in the Commands
field so that you can use it to configure the command.
You can even chain commands to, for example, connect to a remote server and then to a postgres container running in a remote k8s cluster:
[[database]]
Name = 'container'
Provider = 'postgres'
DBName = 'foo'
URL = 'postgres://postgres:password@localhost:${port}/foo'
Commands = [
  { Command = 'ssh -tt remote-bastion -L 6443:localhost:6443', WaitForPort = '6443' },
  { Command = 'kubectl port-forward service/postgres ${port}:5432 --kubeconfig /path/to/kube.conf', WaitForPort = '${port}' }
]
Keybindings
Global
| Key | Action | 
|---|---|
| q | Quit | 
| CTRL + e | Open SQL editor | 
| Backspace | Return to connection selection | 
| ? | Show keybindings popup | 
Table
| Key | Action | 
|---|---|
| c | Edit table cell | 
| d | Delete row | 
| o | Add row | 
| / | Focus the filter input or SQL editor | 
| CTRL + s | Commit changes | 
| > | Next page | 
| < | Previous page | 
| K | Sort ASC | 
| J | Sort DESC | 
| H | Focus tree panel | 
| CTRL+[ | Focus previous tab | 
| CTRL+] | Focus next tab | 
| X | Close current tab | 
| R | Refresh the current table | 
Tree
| Key | Action | 
|---|---|
| L | Focus table panel | 
| G | Focus last database tree node | 
| g | Focus first database tree node | 
| CTRL+u | Scroll 5 items up | 
| CTRL+d | Scroll 5 items down | 
SQL Editor
| Key | Action | 
|---|---|
| CTRL + R | Run the SQL statement | 
| CTRL + Space | Open external editor (Linux only) | 
Specific editor for lazysql can be set by $SQL_EDITOR.
Specific terminal for opening editor can be set by $SQL_TERMINAL
Example connection URLs
postgres://user:pass@localhost/dbname
pg://user:pass@localhost/dbname?sslmode=disable
mysql://user:pass@localhost/dbname
mysql:/var/run/mysqld/mysqld.sock
sqlserver://user:[email protected]/dbname
mssql://user:[email protected]/instance/dbname
ms://user:[email protected]:port/instance/dbname?keepAlive=10
oracle://user:[email protected]/sid
sap://user:pass@localhost/dbname
file:myfile.sqlite3?loc=auto
/path/to/sqlite/file/test.db
odbc+postgres://user:pass@localhost:port/dbname?option1=
Roadmap
- [ ] Support for NoSQL databases
 - [ ] Columns and indexes creation through TUI
 - [x] Table tree input filter
 - [ ] Custom keybindings
 - [x] Show keybindings on a modal
 - [x] Rewrite row 
create,updateanddeletelogic 
See the open issues for a full list of proposed features (and known issues).
(back to top)
Clipboard support
We use atotto/clipboard to copy to clipboard.
Platforms:
- OSX
 - Windows 7 (probably work on other Windows)
 - Linux, Unix (requires 'xclip' or 'xsel' command to be installed)
 
Contributing
Contributions, issues, and pull requests are welcome!
(back to top)
License
Distributed under the MIT License. See LICENSE.txt for more information.
(back to top)
Contact
Jorge Rojas - LinkedIn - [email protected]
(back to top)
Alternatives
(back to top)