Skip to content

Instantly share code, notes, and snippets.

@tappoz
Last active December 9, 2016 17:17
Show Gist options
  • Save tappoz/ec96b2bf4ad4ad7dd7642310cb169ac5 to your computer and use it in GitHub Desktop.
Save tappoz/ec96b2bf4ad4ad7dd7642310cb169ac5 to your computer and use it in GitHub Desktop.
cheatsheet for common tasks using Postgres or MySQL

Postgres

Command to display active connections to a PostgresDB: SELECT * FROM pg_stat_activity;

Connection to Redshift

$ psql -h <ENDPOINT_NAME>.us-east-1.redshift.amazonaws.com -p 5439 -d <DB_NAME> -U <DB_USERNAME>
password <TYPE_YOUR_PASSWORD_HERE>
\list or \l: list all databases
\dt: list all tables in the current database
\dn: list of schemas
\dt <schema_name>.*: list tables in <schema_name>
\d+ <schema_name>.<table_name>: describe a table in a schema
SET search_path = <schema_name>;

To find all the columns with a name like query:

select table_name, column_name 
from information_schema.columns 
where table_schema=<SCHEMA_NAME> and column_name like '%some%thing%';

To see output in VERTICAL form:

\x # enable vertical output
<your queries>
\x # disable vertical output

MySQL

Connection

$ mysql -h <HOST_IP> -P 3306 -u <DB_USERNAME> -p -A -D <DB_NAME>
<TYPE_YOUR_PASSWORD_HERE>
show databases;
use <DB_NAME>;
<YOUR QUERY> \G # the output in VERTICAL form

Performing a dump

$ mysqldump -P 3306 -h <HOST_IP> -u <DB_USERNAME> -p <DB_PASSWORD> <TABLE_NAME> --where="columns='some_value'" > ~/tmp/foo.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment