Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Postgres SQL Scripts - Shorthand Commands for PSQL

Use of .psqlrc

The .psqlrc file allows you to configure personalized settings, add shortcuts and add custom commands. It is stored in the users home directory and will automatically be read upon launch of psql.

An example of a .psqlrc file can be found here:

Ignore .psqlrc
psql --username=<username> --dbname=<database> --host=<hostname> --port=<port> --no-psqlrc

General psql navigation

Connect

Password can be exported at the command line by using:

export PGPASSWORD=<somepassword>

General connections can be made using:

psql -U <username> -d <database> -h <hostname> -p <port>

psql --username=<username> --dbname=<database> --host=<hostname> --port=<port>

psql -U <username> -d <database> -h <host> -f <local_file>

psql --username=<username> --dbname=<database> --host=<host> --file=<local_file>
Disconnect
\q
\!
Clear scrollback
(CTRL + L)
Set Resultset to "Unaligned"
\a
Set Resultset to "Extended"
\x

Information about the connected system

Server version
SHOW SERVER_VERSION;
Show connection information (SSL USED? PSQL VERSION?)
\conninfo
Show environment variables
SHOW ALL;
List all roles in the instance
SELECT rolname FROM pg_roles;
Show currently connected user
SELECT current_user;
Show current user permissions
\du
\du+
Show current user's session settings
\drds
show current database
SELECT current_database();
show all tables in database
\dt
\dt+
list functions
\df <schema>
\df+ <schema>

Databases

list databases with size information
\l
\l+
Connect to database
\c <database_name>
Connect to database as different user
\c <database_name> <username>

Users

create user

http://www.postgresql.org/docs/current/static/sql-createuser.html

CREATE USER <user_name> WITH PASSWORD '<password>';
drop user

http://www.postgresql.org/docs/current/static/sql-dropuser.html

DROP USER IF EXISTS <user_name>;
alter user password

http://www.postgresql.org/docs/current/static/sql-alterrole.html

ALTER ROLE <user_name> WITH PASSWORD '<password>';

Permissions

Grant all permissions on database

http://www.postgresql.org/docs/current/static/sql-grant.html

GRANT ALL PRIVILEGES ON DATABASE <db_name> TO <user_name>;
Grant connect permission on database
GRANT CONNECT ON DATABASE <db_name> TO <user_name>;
Grant individual permissions on schema
GRANT USAGE ON SCHEMA public TO <user_name>;
Grant permissions to functions
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO <user_name>;
Grant permissions to select, update, insert, delete, on all tables in a schema
GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO <user_name>;
Grant all permissions on an individual table
GRANT SELECT, UPDATE, INSERT ON <table_name> TO <user_name>;
Grant a single permission on an individual table
GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user_name>;

Schema

List schemas
\dn

\dn+

SELECT schema_name FROM information_schema.schemata;

SELECT nspname FROM pg_catalog.pg_namespace;
Create a schema

http://www.postgresql.org/docs/current/static/sql-createschema.html

CREATE SCHEMA IF NOT EXISTS <schema_name>;
Drop a schema

http://www.postgresql.org/docs/current/static/sql-dropschema.html

DROP SCHEMA IF EXISTS <schema_name> CASCADE;

Tables

List all tables, in current db (limited by your search_path)
\dt
\dt+

SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;
List tables, globally (not limited by search_path)
\dt *.*

SELECT * FROM pg_catalog.pg_tables
List table schema
\d <table_name>
\d+ <table_name>

SELECT column_name, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '<table_name>';

Columns

Add a column
ALTER TABLE <table_name> IF EXISTS
ADD <column_name> <data_type> [<constraints>];
Change a column datatype
ALTER TABLE <table_name> IF EXISTS
ALTER <column_name> TYPE <data_type> [<constraints>];
Delete a column (beware of table locks)
ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;

Scripting

Export table into CSV file

http://www.postgresql.org/docs/current/static/sql-copy.html

\copy <table_name> TO '<file_path>' CSV
Export table, only specific columns, to CSV file
\copy <table_name>(<column_1>,<column_1>,<column_1>) TO '<file_path>' CSV
Import CSV file into table

http://www.postgresql.org/docs/current/static/sql-copy.html

\copy <table_name> FROM '<file_path>' CSV
Import CSV file into table, only specific columns
\copy <table_name>(<column_1>,<column_1>,<column_1>) FROM '<file_path>' CSV