#!/usr/bin/perl -w # Copyright (C) 2008-2010 Ole Tange, http://ole.tange.dk # # Copyright (C) 2010-2025 Ole Tange, http://ole.tange.dk and # Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see # or write to the Free Software Foundation, Inc., 51 Franklin St, # Fifth Floor, Boston, MA 02110-1301 USA # # SPDX-FileCopyrightText: 2008-2025 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc. # SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GFDL-1.3-or-later =head1 NAME sql - execute a command on a database determined by a dburl =head1 SYNOPSIS B [options] I [I] B [options] I < commandfile B<#!/usr/bin/sql> B<--shebang> [options] I =head1 DESCRIPTION GNU B aims to give a simple, unified interface for accessing databases through all the different databases' command line clients. So far the focus has been on giving a common way to specify login information (protocol, username, password, hostname, and port number), size (database and table size), and running queries. The database is addressed using a DBURL. If I are left out you will get that database's interactive shell. GNU B is often used in combination with GNU B. =over 9 =item I A DBURL has the following syntax: [sql:]vendor:// [[user][:password]@][host][:port]/[database][?sqlquery] See the section DBURL below. =item I The SQL commands to run. Each argument will have a newline appended. Example: "SELECT * FROM foo;" "SELECT * FROM bar;" If the arguments contain '\n' or '\x0a' this will be replaced with a newline: Example: "SELECT * FROM foo;\n SELECT * FROM bar;" If no commands are given SQL is read from the keyboard or STDIN. Example: echo 'SELECT * FROM foo;' | sql mysql:/// =item B<--csv> CSV output. =item B<--db-size> =item B<--dbsize> Size of database. Show the size of the database on disk. For Oracle this requires access to read the table I - the user I has that. =item B<--help> =item B<-h> Print a summary of the options to GNU B and exit. =item B<--html> HTML output. Turn on HTML tabular output. =item B<--json> =item B<--pretty> Pretty JSON output. =item B<--list-databases> =item B<--listdbs> =item B<--show-databases> =item B<--showdbs> List the databases (table spaces) in the database. =item B<--listproc> =item B<--proclist> =item B<--show-processlist> Show the list of running queries. =item B<--list-tables> =item B<--show-tables> =item B<--table-list> List the tables in the database. =item B<--noheaders> =item B<--no-headers> =item B<-n> Remove headers and footers and print only tuples. Bug in Oracle: it still prints number of rows found. =item B<-p> I The string following -p will be given to the database connection program as arguments. Multiple -p's will be joined with space. Example: pass '-U' and the user name to the program: I<-p "-U scott"> can also be written I<-p -U -p scott>. =item B<--precision> > Precision of timestamps. Specifiy the format of the output timestamps: rfc3339, h, m, s, ms, u or ns. =item B<-r> Try 3 times. Short version of I<--retries 3>. =item B<--retries> I Try I times. If the client program returns with an error, retry the command. Default is I<--retries 1>. =item B<--sep> I =item B<-s> I Field separator. Use I as separator between columns. =item B<--skip-first-line> Do not use the first line of input (used by GNU B itself when called with B<--shebang>). =item B<--table-size> =item B<--tablesize> Size of tables. Show the size of the tables in the database. =item B<--verbose> =item B<-v> Print which command is sent. =item B<--version> =item B<-V> Print the version GNU B and exit. =item B<--shebang> =item B<-Y> GNU B can be called as a shebang (#!) command as the first line of a script. Like this: #!/usr/bin/sql -Y mysql:/// SELECT * FROM foo; For this to work B<--shebang> or B<-Y> must be set as the first option. =back =head1 DBURL A DBURL has the following syntax: [sql:]vendor:// [[user][:password]@][host][:port]/[database][?sqlquery] To quote special characters use %-encoding specified in http://tools.ietf.org/html/rfc3986#section-2.1 (E.g. a password containing '/' would contain '%2F'). csv:///%2Ftmp%2Fparallel-bug-56096/mytable csv:////tmp/parallel-bug-56096/mytable mysql://me@/me/ mysql://// sqlite3:///%2Frun%2Fshm%2Fparallel.db sqlite3:///%2Frun%2Fshm%2Fparallel.db/table sqlite:///%2Ftmp%2Ffile.sqlite?SELECT csv:///%2Ftmp%2Fparallel-CSV/OK csv:///%2Fmust%2Ffail/fail sqlite3:///%2Frun%2Fshm%2Fparallel.db Examples: mysql://scott:tiger@my.example.com/mydb influxdb://scott:tiger@influxdb.example.com/foo sql:oracle://scott:tiger@ora.example.com/xe postgresql://scott:tiger@pg.example.com/pgdb pg:/// postgresqlssl://scott@pg.example.com:3333/pgdb sql:sqlite2:////tmp/db.sqlite?SELECT * FROM foo; sqlite3:///../db.sqlite3?SELECT%20*%20FROM%20foo; Currently supported vendors: =over 2 =item * MySQL (mysql) with SSL (mysqls, mysqlssl) =item * Oracle (oracle, ora) =item * PostgreSQL (postgresql, pg, pgsql, postgres) with SSL (postgresqlssl, pgs, pgsqlssl, postgresssl, pgssl, postgresqls, pgsqls, postgress) =item * SQLite2 (sqlite, sqlite2) =item * SQLite3 (sqlite3) =item * InfluxDB 1.x (influx, influxdb) with SSL (influxdbssl, influxdbs, influxs, influxssl) =back Aliases must start with ':' and are read from /etc/sql/aliases and ~/.sql/aliases. The user's own ~/.sql/aliases should only be readable by the user. Example of aliases: :myalias1 pg://scott:tiger@pg.example.com/pgdb :myalias2 ora://scott:tiger@ora.example.com/xe # Short form of mysql://`whoami`:nopassword@localhost:3306/`whoami` :myalias3 mysql:/// # Short form of mysql://`whoami`:nopassword@localhost:33333/mydb :myalias4 mysql://:33333/mydb # Alias for an alias :m :myalias4 # the sortest alias possible : sqlite2:////tmp/db.sqlite # Including an SQL query :query sqlite:////tmp/db.sqlite?SELECT * FROM foo; =head1 EXAMPLES =head2 Get an interactive prompt The most basic use of GNU B is to get an interactive prompt: B If you have setup an alias you can do: B =head2 Run a query To run a query directly from the command line: B Oracle requires newlines after each statement. This can be done like this: B Or this: B =head2 Copy a PostgreSQL database To copy a PostgreSQL database use pg_dump to generate the dump and GNU B to import it: B =head2 Empty all tables in a MySQL database Using GNU B it is easy to empty all tables without dropping them: B =head2 Drop all tables in a PostgreSQL database To drop all tables in a PostgreSQL database do: B =head2 Run as a script Instead of doing: B you can combine the sqlfile with the DBURL to make a UNIX-script. Create a script called I: B<#!/usr/bin/sql -Y mysql:///> B