NAME

sql - execute a command on a database determined by a dburl


SYNOPSIS

sql [-hnr] [--table-size] [--db-size] [-p pass-through] [-s string] dburl [sqlcommand]

#!/usr/bin/sql --shebang [options] dburl


DESCRIPTION

GNU sql 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 command is left out you will get that database's interactive shell.

GNU sql is often used in combination with GNU parallel.

dburl

A DBURL has the following syntax: vendor://[user[:password]@][host][:port]/[database]

See the section DBURL below.

command

The SQL command to run.

Example: select 1+2

If you use * or ? remember to quote them, as the shell may otherwise expand them.

If no command is given SQL is read from the keyboard or STDIN.

Example: echo 'select 1+2' | sql mysql:///

--db-size
--dbsize

Size of database. Show the size of the database on disk.

--help
-h

Print a summary of the options to GNU sql and exit.

--html

HTML output. Turn on HTML tabular output.

--show-processlist
--proclist
--listproc

Show the list of running queries.

--noheaders
--no-headers
-n

Remove headers and footers and print only tuples. Bug in Oracle: it still prints number of rows found.

-p pass-through

The string following -p will be given to the database connection program as arguments. Multiple -p's will be joined with space. Example:

-p "-U username" can also be written -p -U -p username.

-r

Try 3 times. Short version of --retries 3.

--retries ntimes

Try ntimes times. If the client program returns with an error, retry the command. Default is --retries 1.

--sep string
-s string

Field separator. Use string as seperator between columns.

--skip-first-line

Do not use the first line of input (used by GNU sql itself when called with --shebang).

--table-size
--tablesize

Size of tables. Show the size of the tables in the database.

--version
-V

Print the version GNU sql and exit.

--shebang
-Y

GNU sql can be called as a shebang (#!) command as the first line of a script. Like this:

  #!/usr/bin/sql -Y mysql:///
  select * from users;

For this to work --shebang or -Y must be set as the first option.


DBURL

A DBURL has the following syntax: vendor://[user[:password]@][host][:port]/[database]

Examples: mysql://user:pa55w0rd@mysqlserver/database oracle://scott:tiger@oracleserver/xe postgresql://user:pa55w0rd@pg.example.com/db pg:///

Currently supported vendors: MySQL (mysql), MySQL with SSL (mysqls, mysqlssl), Oracle (oracle, ora), PostgreSQL (postgresql, pg, pgsql, postgres), PostgreSQL with SSL (postgresqlssl, pgs, pgsqlssl, postgresssl, pgssl, postgresqls, pgsqls, postgress)

Aliases must start with ':' and is read from dburl.aliases.dist, dburl.aliases and ~/.dburl.aliases. The user's own ~/.dburl.aliases should only be readable by the user.

Example of dburl.aliases:

 :myalias1 pg://user:pa55w0rd@pg.example.com/db
 :myalias2 ora://scott:tiger@ora1.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


EXAMPLES

Copy a PostgreSQL database

pg_dump my_database | sql pg://user:pass@pgserver/my_new_db

Empty all tables in a MySQL database

sql -n mysql:/// 'show tables' | parallel sql mysql:/// delete from {};

Drop all tables in a PostgreSQL database

sql -n pg:/// '\dt' | awk '{print $3}' | parallel -r sql pg:/// drop table {};

Run as a script

Create a script called demosql:

 #!/usr/bin/sql -Y mysql:///
 select * from users;

Then do:

 chmod 755 demosql; ./demosql

Use --colsep to process multiple columns

Use GNU parallel's --colsep to separate columns:

 sql :mydburl 'select * from bar' | parallel --colsep '\t' do_stuff {4} {1}


REPORTING BUGS

GNU sql is part of GNU parallel. Report bugs to <bug-parallel@gnu.org>.


AUTHOR

Copyright (C) 2008,2009,2010 Ole Tange http://ole.tange.dk


LICENSE

Copyright (C) 2007,2008,2009,2010 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 <http://www.gnu.org/licenses/>.

Documentation license I

Permission is granted to copy, distribute and/or modify this documentation under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the file fdl.txt.

Documentation license II

You are free:

to Share

to copy, distribute and transmit the work

to Remix

to adapt the work

Under the following conditions:

Attribution

You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

Share Alike

If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

With the understanding that:

Waiver

Any of the above conditions can be waived if you get permission from the copyright holder.

Public Domain

Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.

Other Rights

In no way are any of the following rights affected by the license:

Notice

For any reuse or distribution, you must make clear to others the license terms of this work.

A copy of the full license is included in the file as cc-by-sa.txt.


DEPENDENCIES

GNU sql uses Perl. If mysql is installed, MySQL dburls will work. If psql is installed, PostgreSQL dburls will work. If sqlplus is installed, Oracle dburls will work. If rlwrap is installed, GNU sql will have a command history for Oracle.


FILES

~/.dburl.aliases - user's own aliases with DBURLs

dburl.aliases - user's own aliases with DBURLs

dburl.aliases.dist - common aliases with DBURLs


SEE ALSO

mysql(1), psql(1), sqlplus(1), rlwrap(1)