Skip to content

Instantly share code, notes, and snippets.

View bitlather's full-sized avatar

Jun Reiderdai bitlather

View GitHub Profile
PostgreSQL 9.3.14 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bit
sudo su - postgres
Run 'psql' to connect, or 'psql -E' to connect and show psql queries.
Commands:
\l | List databases
\connect db_name | Use db_name database
\dt | List tables
\dv | List views
-- PostgreSQL 9.3.14 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bit
-- ============================================================================
-- Returns the comments for all columns in a table.
--
-- Precondition:
--
-- Assumes you are currently using a database.
--
-- Usage:
--
-- PostgreSQL 9.3.14 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bit
-- ============================================================================
-- Create a view to easily read table comments.
--
-- Returns:
--
-- A list of table names and comments associated with them.
--
-- Usage:
--
-- PostgreSQL 9.3.14 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bit
-- ============================================================================
-- Returns the comments for all functions we'll create.
--
-- Precondition:
--
-- Assumes you are currently using a database.
--
-- Usage:
--
@bitlather
bitlather / postgresql_insert_or_select.sql
Last active October 20, 2017 20:11
PostgreSQL function that inserts only if does not exist and returns row id.
-- PostgreSQL 9.3.14 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4, 64-bit
CREATE TABLE func_table (
id serial NOT NULL PRIMARY KEY,
value integer NOT NULL
);
--
-- Make column unique.
--
ALTER TABLE func_table ADD CONSTRAINT unique_value UNIQUE (value);