Skip to content

Instantly share code, notes, and snippets.

@csaura
Forked from apolloclark/postgres cheatsheet.md
Created June 2, 2017 22:19
Show Gist options
  • Select an option

  • Save csaura/9cf6ab4894a30b4a33dcbe24da5eaae3 to your computer and use it in GitHub Desktop.

Select an option

Save csaura/9cf6ab4894a30b4a33dcbe24da5eaae3 to your computer and use it in GitHub Desktop.
postgres cheatsheet

Posgres Cheatsheet

This is a collection of the most common commands I run while administering Postgres databases.

Setup

installation

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
connect
@see http://www.postgresql.org/docs/9.2/static/app-psql.html
psql --username=<username> --dbname=<database> --host=<hostname>

psql -U <username> -d <database> -h <hostname>
info
\conninfo
disconnect
\q
\!




Databases

list databasees
\l
connect to database
\c <database_name>
create datacase

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

CREATE DATABASE <database_name> WITH OWNER <username>;
delete database

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

DROP DATABASE IF EXISTS <database_name>;




Users

list roles
SELECT rolname FROM pg_roles;
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 connection permissions on database
GRANT CONNECT ON DATABASE <db_name> TO <user_name>;
grant 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 a all tables
GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO <user_name>;
grant permissions, on a table
GRANT SELECT, UPDATE, INSERT ON <table_name> TO <user_name>;
grant permissions, to select, on a table
GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user_name>;




Schema

list schemas
\dn

SELECT schema_name FROM information_schema.schemata;

SELECT nspname FROM pg_catalog.pg_namespace;
create schema

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

CREATE SCHEMA IF NOT EXISTS <schema_name>;
drop schema

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

DROP SCHEMA IF EXISTS <schema_name> CASCADE;




Tables

list tables, in current db
\dt

SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;
list tables, globally
\dt *.*.

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

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

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

CREATE TABLE <table_name>(
  <column_name> <column_type>,
  <column_name> <column_type>
);
list table schema
\d <table_name>
delete table

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

DROP TABLE IF EXISTS <table_name> CASCADE;
add column

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

ALTER TABLE <table_name> IF EXISTS
ADD <column_name> <data_type> [<constraints>];
update column
ALTER TABLE <table_name> IF EXISTS
ALTER <column_name> TYPE <data_type> [<constraints>];
delete column
ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;




Data

add / create / insert table into table

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

INSERT INTO <table_name> VALUES( <value_1>, <value_2> );
read all data

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

SELECT * FROM <table_name>;
read one row of data
SELECT * FROM <table_name> LIMIT 1;
search for data
SELECT * FROM <table_name> WHERE <column_name> = <value>;
update / edit data

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>;
delete data

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

DELETE FROM <table_name>
WHERE <column_name> = <value>;




Scripting

run local script
psql -U <username> -d <database> -h <host> -f <local_file>
backup database data

http://www.postgresql.org/docs/current/static/app-pgdump.html

pg_dump -a <database_name>

pg_dump --data-only <database_name>
backup database schema
pg_dump -s <database_name>

pg_dump --schema-only <database_name>
restore database data
restore database schema
pg_restore -d somedb dump.dmp
import CSV file into table

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

COPY <table_name> FROM '<file_path>' DELIMITER ',' CSV;

\copy <table_name> FROM '<file_path>' DELIMITER ',' CSV
import CSV file into table, only specific columns
COPY <table_name>(<column_1>,<column_1>,<column_1>) FROM '<file_path>' DELIMITER ',' CSV;

\copy <table_name>(<column_1>,<column_1>,<column_1>) FROM '<file_path>' DELIMITER ',' CSV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment