All notes
Postgresql

Command line client

psql.



psql -U username -d dbname

psql "dbname=me host=hosthere user=userhere password=passwordhere port=5432 sslmode=require"

\?  # Help.
\l      # List all d's
\c dbname  # Connect to a database.
\d dbname  # Describe a db.
\dt   # Show tables.

Commands on server

createuser



createuser joe
# Shall the new role be a superuser? (y/n) n
# Shall the new role be allowed to create databases? (y/n) n
# Shall the new role be allowed to create more new roles? (y/n) n

# To create the same user joe using the server on host eden, port 5000, avoiding the prompts and taking a look at the underlying command:
# -S, --no-superuser.
# -e, --echo. Echo the commands.
createuser -h eden -p 5000 -S -D -R -e joe
# CREATE ROLE joe NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;

# To create the user joe as a superuser, and assign a password immediately:
# -P, --pwprompt.
# -s, --superuser.
createuser -P -s -e joe
# Enter password for new role: xyzzy
# Enter it again: xyzzy
# CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;



# List all database user accounts
\du

# Quit
\q