usql

repository·main·Indexed 27 days ago

https://github.com/xo/usql

A universal command-line interface for interacting with a wide variety of SQL and NoSQL databases, inspired by PostgreSQL's psql. It features support for multiple database drivers (including PostgreSQL, MySQL, SQLite3, Microsoft SQL Server, Oracle, and ODBC), syntax highlighting, and the ability to copy data between different databases using the \copy command.

Tokens
6.6K
Snippets
26
Records
39
Agent score
88%

What's inside usql

  1. Understand usql Release Builds

    main
    Official release builds of usql are compiled using the most build tag and include additional SQLite3 build tags. This ensures that the binaries come pre-equipped with support for a wide variety of database drivers.
  2. Install usql via AUR (Arch Linux)

    main

    On Arch Linux, you can install usql using an AUR helper like yay, or by manually building the package using makepkg from the cloned AUR repository.

    # install usql with most drivers
    $ yay -S usql
    
    # build and install using makepkg
    $ git clone https://aur.archlinux.org/usql.git && cd usql
    $ makepkg -si
  3. Use .usqlpass for database passwords

    main

    To avoid providing passwords in connection strings, you can use a .usqlpass file in your $HOME directory.

    Format: protocol:host:port:dbname:user:pass

    Example: postgres:*:*:*:booktest:booktest

    Security Note: Ensure the file is not readable by other users: chmod 0600 ~/.usqlpass

  4. Install usql via Release

    main
    To install usql manually, download the appropriate release for your platform from the official GitHub releases page. Extract the usql or usql.exe file from the .tar.bz2 or .zip archive and move the executable to a directory included in your $PATH (Linux/macOS) or %PATH% (Windows).
  5. Use usql to connect to databases

    main

    usql connects to databases using a Data Source Name (DSN) or a connection URL. You can pass the DSN as a command-line argument or use the \c command within the interactive interpreter.

    Connection String Formats

    • URL style: driver+transport://user:pass@host/dbname?opt1=a&opt2=b
    • Path style: /path/to/file (used for local files like SQLite or directory-based databases like PostgreSQL)
    • Named connection: A name defined in your config.yaml or via \cset.

    Driver Aliases

    Most drivers have two-character short aliases:

    • pg for postgres
    • my for mysql
    • ms for sqlserver
    • or for oracle
    • sq for sqlite3
    # connect to a postgres database
    $ usql postgres://booktest@localhost/booktest
    
    # connect to an oracle database
    $ usql oracle://user:pass@host/oracle.sid
    
    # connect to a postgres database and run the commands contained in script.sql
    $ usql pg://localhost/ -f script.sql
  6. Install usql via Scoop (Windows)

    main

    To install usql on Windows using Scoop, ensure your execution policy allows remote scripts, install Scoop if necessary, and then run the install command.

    # Optional: Needed to run a remote script the first time
    > Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    
    # install scoop if not already installed
    > irm get.scoop.sh | iex
    
    # install usql with scoop
    > scoop install usql
  7. Run and verify DB2 container

    main

    To test the DB2 environment, run the DB2 container using the provided podman script and then execute the validation script to ensure the driver and configuration are working correctly.

    Run the container:

    $ ../podman-run.sh db2 -u

    Verify the installation:

    $ ./db2cli-validate.sh
    $ ../podman-run.sh db2 -u
    $ ./db2cli-validate.sh
  8. Connect to a database in usql

    main
    You can connect to a database by providing a connection URL (DSN) directly as a command-line argument or by using the \connect (or \c) command inside the usql interactive session. Use the appropriate scheme or one of its aliases.
  9. Run usql via Docker

    main

    Use the official docker.io/usql/usql image to run usql in a container. This is useful for connecting to databases without local installation or for running specific versions.

    # run interactive shell and mount the $PWD/data directory as a volume for use
    # within the container
    $ docker run --rm -it --volume $(pwd)/data:/data docker.io/usql/usql:latest sqlite3://data/test.db
    
    # connect to local postgres instance (assuming postgres is running on host network)
    $ docker run --rm --network host -it docker.io/usql/usql:latest postgres://postgres:P4ssw0rd@localhost
    
    # run specific usql version
    $ docker run --rm -it docker.io/usql/usql:0.19.3
  10. Install usql via Go

    main

    You can install usql using go install. By default, only the base drivers (PostgreSQL, MySQL, SQLite3, Microsoft SQL Server, Oracle, CSVQ) are included. To include a wider range of drivers, use the most build tag.

    # install latest usql version with base drivers
    $ go install github.com/xo/usql@latest
    
    # install usql with most drivers
    $ go install -tags most github.com/xo/usql@latest
  11. Access the Couchbase Web UI

    main

    Once the Couchbase Docker image is running, you can access the management interface by navigating to the following URL in your browser:

    http://127.0.0.1:8091/ui/index.html

    You must manually configure the database through this interface.