tips
40 tips SQL shells
One command, setting or habit at a time about SQL shells: what it does, the line to copy and a caveat or two.
SQL shells 40
-
Takes every value a query returns and executes it as its own SQL statement.
\gexec -
Runs one SQL statement automatically after the client connects, and again after a reconnect.
mariadb --init-command="SET SESSION sql_mode='STRICT_ALL_TABLES'" mydb -
Surfaces truncated and altered values that a quiet import would hide.
mariadb --show-warnings -u root -p shop < data.sql -
Stores host, user and password so you stop typing them in commands.
mysql_config_editor set --login-path=shop \ -
Makes a SQL script stop at the first failing statement instead of running the rest.
sqlite3 -bail app.db < migrate.sql -
Repeats the last server error with its SQLSTATE code, details and source location.
appdb=# \errverbose -
Prints a marker for NULL values so you can tell them apart from empty text.
\pset null '(null)' -
Records every query and result of an interactive session in a log file.
MariaDB [shop]> tee /tmp/fix-orders.log -
Writes one query result to a file from inside the shell, then output goes back to the screen.
sqlite> .once users.csv -
Stores PostgreSQL passwords per host and user in a private file that all client tools read.
echo 'db1.example.com:5432:*:backup:change-me' >> ~/.pgpass -
Gets back ids and values of changed rows from the same statement that changed them.
DELETE FROM users WHERE active = 0 RETURNING id, email; -
Sends query output through less without line wrapping, so wide tables stay aligned.
MariaDB [shop]> pager less -S -
Creates thousands of realistic test rows with one statement to try an index or a query.
CREATE TABLE readings (id int, sensor int, taken_at timestamptz, value numeric); -
Writes the CREATE and INSERT statements for just one table to move it to another file.
sqlite3 app.db ".dump users" > users.sql -
Pulls fields out of a JSON column with short arrow syntax instead of function calls.
SELECT prefs ->> 'theme', prefs -> 'theme', prefs ->> '$.lang' -
Inserts a row or updates the existing one in a single statement, without a SELECT first.
INSERT INTO counter VALUES ('home', 1) -
Binds values to :name placeholders so you can run an app's query exactly as written.
sqlite> .parameter set :mail '[email protected]' -
Prints a complete CREATE TABLE statement with keys, engine and charset for one table.
SHOW CREATE TABLE orders\G -
Opens a second SQLite file under a name so you can join or copy between both.
sqlite> ATTACH 'old.db' AS old; -
Puts the login user, server host and current database in the client prompt.
mariadb --prompt='\u@\h [\d]> ' shop -
Picks the gentle or the hard way to stop a runaway query from another session.
SELECT pid, now() - query_start AS running, left(query, 60) -
Edits a long query in your own editor and runs it when you save and quit.
appdb=# \e -
Stores the client login in a private file so commands and cron jobs need no -p.
cat > ~/.my.cnf <<'EOF' -
Prints the SQL that psql sends for backslash commands like \dt.
psql -E -d mydb -
Makes the MariaDB client print wide query results vertically, one column per line.
mariadb --auto-vertical-output -u root -p appdb -
Stores the single row of a query result in psql variables for later statements.
\gset -
Shows the source of a PostgreSQL function with numbered lines, to match an error message.
\sf+ my_function -
Runs a SQL script as one transaction and stops at the first error, rolling everything back.
psql -1 -v ON_ERROR_STOP=1 -d appdb -f migrate.sql -
Writes a query result to a CSV file on your own computer, without server file permissions.
\copy (SELECT id, email FROM users WHERE active) TO 'users.csv' WITH (FORMAT csv, HEADER) -
Runs one query and prints plain tab-separated rows without borders or a header line.
mariadb -B -N -e "SELECT id, email FROM users WHERE active" appdb -
Starts psql without reading startup files, so scripts give the same output on every machine.
psql -X -d appdb -f report.sql -
Keeps an open psql transaction usable after one statement fails, instead of aborting all of it.
\set ON_ERROR_ROLLBACK interactive -
Starts the mysql client in a mode that refuses UPDATE and DELETE statements that could hit the whole table.
mysql --safe-updates -u root -p -
Prints the details of the current psql connection, so you know where a query is about to run.
postgres=# \conninfo -
Opens an SQLite database file in read-only mode, so a stray UPDATE or DELETE just returns an error.
sqlite3 -readonly db.sqlite -
Shows sqlite3 query results as aligned columns with their names above them.
.mode column -
Makes psql print wide query results one field per line, only when they do not fit.
\x auto -
Exports SQLite query results to a CSV file and imports a CSV back with the shell.
sqlite3 -header -csv app.db "SELECT id, email FROM users" > users.csv -
Shows disk usage per table, index and database from the psql prompt.
\dt+ -
Reruns the last PostgreSQL query at a fixed interval inside psql.
\watch 2
Nothing in this filter yet.