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:
- Sample .psqlrc file for example .psqlrc content.
psql --username=<username> --dbname=<database> --host=<hostname> --port=<port> --no-psqlrcPassword 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>\q
\!(CTRL + L)\a\xSHOW SERVER_VERSION;
\conninfoSHOW ALL;SELECT rolname FROM pg_roles;SELECT current_user;\du
\du+\drdsSELECT current_database();\dt
\dt+\df <schema>
\df+ <schema>\l
\l+\c <database_name>\c <database_name> <username>http://www.postgresql.org/docs/current/static/sql-createuser.html
CREATE USER <user_name> WITH PASSWORD '<password>';http://www.postgresql.org/docs/current/static/sql-dropuser.html
DROP USER IF EXISTS <user_name>;http://www.postgresql.org/docs/current/static/sql-alterrole.html
ALTER ROLE <user_name> WITH PASSWORD '<password>';http://www.postgresql.org/docs/current/static/sql-grant.html
GRANT ALL PRIVILEGES ON DATABASE <db_name> TO <user_name>;GRANT CONNECT ON DATABASE <db_name> TO <user_name>;GRANT USAGE ON SCHEMA public TO <user_name>;GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO <user_name>;GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO <user_name>;GRANT SELECT, UPDATE, INSERT ON <table_name> TO <user_name>;GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user_name>;\dn
\dn+
SELECT schema_name FROM information_schema.schemata;
SELECT nspname FROM pg_catalog.pg_namespace;http://www.postgresql.org/docs/current/static/sql-createschema.html
CREATE SCHEMA IF NOT EXISTS <schema_name>;http://www.postgresql.org/docs/current/static/sql-dropschema.html
DROP SCHEMA IF EXISTS <schema_name> CASCADE;\dt
\dt+
SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;\dt *.*
SELECT * FROM pg_catalog.pg_tables\d <table_name>
\d+ <table_name>
SELECT column_name, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '<table_name>';ALTER TABLE <table_name> IF EXISTS
ADD <column_name> <data_type> [<constraints>];ALTER TABLE <table_name> IF EXISTS
ALTER <column_name> TYPE <data_type> [<constraints>];ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;http://www.postgresql.org/docs/current/static/sql-copy.html
\copy <table_name> TO '<file_path>' CSV\copy <table_name>(<column_1>,<column_1>,<column_1>) TO '<file_path>' CSVhttp://www.postgresql.org/docs/current/static/sql-copy.html
\copy <table_name> FROM '<file_path>' CSV\copy <table_name>(<column_1>,<column_1>,<column_1>) FROM '<file_path>' CSV