Magic words:
psql -U postgres
If run with -E
flag, it will describe the underlaying queries of the \
commands (cool for learning!).
Most \d
commands support additional param of __schema__.name__
and accept wildcards like *.*
\q
: Quit/Exit\c __database__
: Connect to a database\d __table__
: Show table definition including triggers\dt *.*
: List tables from all schemas (if*.*
is omitted will only show SEARCH_PATH ones)\l
: List databases\dn
: List schemas\df
: List functions\dv
: List views\df+ __function__
: Show function SQL code.\x
: Pretty-format query results instead of the not-so-useful ASCII tables
User Related:
\du
: List users\du __username__
: List a username if present.create role __test1__
: Create a role with an existing username.create role __test2__ noinherit login password __passsword__;
: Create a role with username and password.set role __test__;
: Change role for current session to__test__
.grant __test2__ to __test1__;
: Allow__test1__
to set its role as__test2__
.
- Service management commands:
sudo service postgresql stop
sudo service postgresql start
sudo service postgresql restart
- Changing verbosity & querying Postgres log:
1) First edit the config file, set a decent verbosity, save and restart postgres:
sudo vim /etc/postgresql/9.3/main/postgresql.conf
# Uncomment/Change inside:
log_min_messages = debug5
log_min_error_statement = debug5
log_min_duration_statement = -1
sudo service postgresql restart
- Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
tail -f /var/log/postgresql/postgresql-9.3-main.log
- How to add user who executed a PG statement to log (editing
postgresql.conf
):
log_line_prefix = '%t %u %d %a '
SELECT * FROM pg_proc WHERE proname='__procedurename__'
: List procedure/functionSELECT * FROM pg_views WHERE viewname='__viewname__';
: List view (including the definition)SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));
: Show DB table space in useSELECT pg_size_pretty(pg_database_size('__database_name__'));
: Show DB space in useshow statement_timeout;
: Show current user's statement timeoutSELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';
: Show table indexes- Get all indexes from all tables of a schema:
SELECT
t.relname AS table_name,
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a,
pg_namespace n
WHERE
t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND a.attnum = ANY(ix.indkey)
AND t.relnamespace = n.oid
AND n.nspname = 'kartones'
ORDER BY
t.relname,
i.relname
- Execution data:
- Queries being executed at a certain DB:
SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query
FROM pg_stat_activity
WHERE datname='__database_name__';
- Get all queries from all dbs waiting for data (might be hung):
SELECT * FROM pg_stat_activity WHERE waiting='t'
- Currently running queries with process pid:
SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
pg_stat_get_backend_activity(s.backendid) AS current_query
FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
Casting:
CAST (column AS type)
orcolumn::type
'__table_name__'::regclass::oid
: Get oid having a table name
Query analysis:
EXPLAIN __query__
: see the query plan for the given queryEXPLAIN ANALYZE __query__
: see and execute the query plan for the given queryANALYZE [__table__]
: collect statistics
- pg-top:
top
for PG.sudo apt-get install ptop
+pg_top
- Unix-like reverse search in psql:
$ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc
$ source $HOME/.editrc
This is a collection of the most common commands I run while administering Postgres databases. The variables shown between the open and closed tags, "<" and ">", should be replaced with a name you choose. Postgres has multiple shortcut functions, starting with a forward slash, "". Any SQL command that is not a shortcut, must end with a semicolon, ";". You can use the keyboard UP and DOWN keys to scroll the history of previous commands you've run.
http://www.postgresql.org/download/linux/ubuntu/ https://help.ubuntu.com/community/PostgreSQL
sudo echo "deb http://apt.postgresql.org/pub/repos/apt/ wily-pgdg main" > \
/etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-9.5 postgresql-client-9.5 postgresql-contrib-9.5
sudo su - postgres
psql
http://www.postgresql.org/docs/current/static/app-psql.html
psql
psql -U <username> -d <database> -h <hostname>
psql --username=<username> --dbname=<database> --host=<hostname>
\q
\!
(CTRL + L)
\conninfo
http://www.postgresql.org/docs/current/static/runtime-config.html
sudo nano $(locate -l 1 main/postgresql.conf)
sudo service postgresql restart
# print the last 24 lines of the debug log
sudo tail -24 $(find /var/log/postgresql -name 'postgresql-*-main.log')
SHOW SERVER_VERSION;
\conninfo
SHOW ALL;
SELECT rolname FROM pg_roles;
SELECT current_user;
\du
\l
SELECT current_database();
\dt
\df <schema>
\l
\c <database_name>
SELECT current_database();
http://www.postgresql.org/docs/current/static/sql-createdatabase.html
CREATE DATABASE <database_name> WITH OWNER <username>;
http://www.postgresql.org/docs/current/static/sql-dropdatabase.html
DROP DATABASE IF EXISTS <database_name>;
http://www.postgresql.org/docs/current/static/sql-alterdatabase.html
ALTER DATABASE <old_name> RENAME TO <new_name>;
SELECT rolname FROM pg_roles;
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>';
sudo su - postgres
psql
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
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
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>';
http://www.postgresql.org/docs/current/static/sql-createtable.html
CREATE TABLE <table_name>(
<column_name> <column_type>,
<column_name> <column_type>
);
CREATE TABLE <table_name> (
<column_name> SERIAL PRIMARY KEY
);
http://www.postgresql.org/docs/current/static/sql-droptable.html
DROP TABLE IF EXISTS <table_name> CASCADE;
http://www.postgresql.org/docs/current/static/sql-altertable.html
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>;
ALTER TABLE <table_name>
ADD COLUMN <column_name> SERIAL PRIMARY KEY;
INSERT INTO <table_name>
VALUES (DEFAULT, <value1>);
INSERT INTO <table_name> (<column1_name>,<column2_name>)
VALUES ( <value1>,<value2> );
http://www.postgresql.org/docs/current/static/sql-select.html
SELECT * FROM <table_name>;
SELECT * FROM <table_name> LIMIT 1;
SELECT * FROM <table_name> WHERE <column_name> = <value>;
http://www.postgresql.org/docs/current/static/sql-insert.html
INSERT INTO <table_name> VALUES( <value_1>, <value_2> );
http://www.postgresql.org/docs/current/static/sql-update.html
UPDATE <table_name>
SET <column_1> = <value_1>, <column_2> = <value_2>
WHERE <column_1> = <value>;
http://www.postgresql.org/docs/current/static/sql-delete.html
DELETE FROM <table_name>;
DELETE FROM <table_name>
WHERE <column_name> = <value>;
http://www.postgresql.org/docs/current/static/app-psql.html
psql -U <username> -d <database> -h <host> -f <local_file>
psql --username=<username> --dbname=<database> --host=<host> --file=<local_file>
http://www.postgresql.org/docs/current/static/app-pgdump.html
pg_dump <database_name>
pg_dump <database_name>
pg_dump -a <database_name>
pg_dump --data-only <database_name>
pg_dump -s <database_name>
pg_dump --schema-only <database_name>
http://www.postgresql.org/docs/current/static/app-pgrestore.html
pg_restore -d <database_name> -a <file_pathway>
pg_restore --dbname=<database_name> --data-only <file_pathway>
pg_restore -d <database_name> -s <file_pathway>
pg_restore --dbname=<database_name> --schema-only <file_pathway>
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>' CSV
http://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
http://www.postgresql.org/docs/current/static/using-explain.html
http://www.postgresql.org/docs/current/static/runtime-config-logging.html
http://www.tutorialspoint.com/postgresql/postgresql_constraints.htm
ActiveRecord::Base.connection.execute(<<-SQL
UPDATE coaches
SET
preferences->>'current_sport' = preferences->>'initcap(current_sport)'
SQL
)