SQL Cheat Sheet
Postgress
Connect to Database
HOST="host-url.com"
PORT=5432
USER="username"
DATABASE="employees"
PGPASSWORD='<enter password> psql -h $HOST -p $PORT -U $USER -d $DATABASE
List all Databases
Connect to database
Show current database
SELECT current_database();
Create database
CREATE DATABASE <database_name> WITH OWNER <username>;
Drop database
DROP DATABASE IF EXISTS <database_name>;
Rename database
ALTER DATABASE <old_name> RENAME TO <new_name>;
Show version
Show system status
Show environmental variables
List users
SELECT rolname FROM pg_roles;
Show current user
Show current user's permissions
Show all tables in database
Describe table
Describe table with details
List tables from current schema
Use pg_dump to backup a database
HOST="host-url.com"
PORT=5432
USER="username"
DATABASE="employees"
pg_dump --schema-only -U $USER -d $DATABASE -h $HOST -p $PORT > pgdump_file.sql