Skip to content

Instantly share code, notes, and snippets.

@yihyang
Last active January 25, 2016 08:04
Show Gist options
  • Save yihyang/f90c725d82d3073eb88e to your computer and use it in GitHub Desktop.
Save yihyang/f90c725d82d3073eb88e to your computer and use it in GitHub Desktop.
Commonly used psql commands
# Show all users
\du
# List all the databases
\l
\list
# Connect to database
\c <database_name>
\connect <database_name>
# List all tables within database
\dt
# Login database with another user, with prompt for password
psql -d <dbname> -U <username> -W
psql -h <hostname> -d <dbname> -U <username> -W
## Commands:
# Create User
CREATE USER <username> ENCRYPTED PASSWORD 'password';
create user "username" with password 'password';
# Create new database
CREATE DATABASE <database_name>;
# Change ownership of database
ALTER DATABASE <database_name> OWNER TO <username>;
# DROP DATABASE
DROP DATABASE IF EXISTS <table_name>;
# DROP TABLES
DROP TABLE IF EXISTS <table_name>;
References:
http://www.postgresql.org/docs/9.1/static/app-createuser.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment