SQL Database Uses

Mongo DB

mongo --host localhost --port 27017

show dbs;

use backup;

show collections;
user

db.user.find() 

sqlite3

.tables
.schema table_name
SELECT * FROM table_name;
.exit

MySQL

MSSQL

Select version

Show all databases

master, tempdb, model, and msdb are default databases.

Selecting database and showing available tables

Selecting table and showing all columns

Oneliner for Enabling xp_cmdshell and executing commands

Postgresql

Reference : https://gist.github.com/Kartones/dd3ff5ec5ea238d4c546

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)

  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

  • \?: Show help (list of available commands with an explanation)

  • \q: Quit/Exit

  • \c __database__: Connect to a database

  • \d __table__: Show table definition (columns, etc.) including triggers

  • \d+ __table__: More detailed table definition including description and physical disk size

  • \l: List databases

  • \dy: List events

  • \df: List functions

  • \di: List indexes

  • \dn: List schemas

  • \dt *.*: List tables from all schemas (if *.* is omitted will only show SEARCH_PATH ones)

  • \dT+: List all data types

  • \dv: List views

  • \dx: List all extensions installed

  • \df+ __function__ : Show function SQL code.

  • \x: Pretty-format query results instead of the not-so-useful ASCII tables

  • \copy (SELECT * FROM __table_name__) TO 'file_path_and_name.csv' WITH CSV: Export a table as CSV

  • \des+: List all foreign servers

  • \dE[S+]: List all foreign tables

  • \! __bash_command__: execute __bash_command__ (e.g. \! ls)

Last updated

Was this helpful?