PostgreSQL Cheatsheet

Table of Contents

PostgreSQL is a relational database.

Shell Commands

createdb <dbname>                               # create a database
dropdb <dbname>                                 # delete a database
psql                                            # login to default database & default user
psql -d <dbname> -f <filename>                  # connect to the specified database and run a SQL script
psql -h <hostname> -U <username> -d <dbname>    # login to remote database

psql Commands

CommandDescription
\llist all databases
\c <database>connect to
\dlist all tables and other objects
\dtlist all tables
\dulist all users and their roles
\d <table_name>list details about <table_name>
\hshow help
\qquit
CREATE USER <username> with encrypted password 'my-password'create a user
ALTER USER <username> SUPERUSER;make a user an admin user
CREATE DATABASE <username>;create a database
GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>;grant all privileges

References