Skip to content

Instantly share code, notes, and snippets.

@tannakartikey
Last active March 23, 2017 14:44
Show Gist options
  • Save tannakartikey/2ad111051d43200d94f670c2d6fac90c to your computer and use it in GitHub Desktop.
Save tannakartikey/2ad111051d43200d94f670c2d6fac90c to your computer and use it in GitHub Desktop.
I am going to put all the commands I use while playing with Postgres. So that you don't have Google it frequently like I did.

List of Postgress Commands

Postgres Cheatsheet

All you might need while playing with Postgres

psql -U <username> : login using console (Windows)
'CREATE DATABASE <db_name>; : create database
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name='<your_table_name>' ORDER BY column_name ASC; : list all table columns in alphabetical(sorted) order

\c : Connect to database
\l : List database
\d+ : Show detailed list of all tables
\dt : Show list of all tables in a selected database
\d : List all tables in a database (almost similar output as \dt)
\du : List all users
CREATE USER <user_name> WITH PASSWORD '<user_password>';: Create user with password
GRANT ALL PRIVILEGES ON DATABASE <db_name> to <user_name>;: Grant all privileges of a database to a user createuser -s <user_name> : Creates a superuser(shell command)
alter table <table_name> rename to <new_table_name> : Rename table alter database <db_name> rename to <new_db_name>: Rename database alter database <db_name> owner to <new_user_name>: Change database owner ALTER USER <user_name> WITH PASSWORD '<new_password>';: Change password of a user. Ordinary users can only change their own password.

Weird Behaviour

createdb <db_name> -U <username> does not work. createdb -U <username> <db_name> works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment