Skip to content

Instantly share code, notes, and snippets.

View slardiere's full-sized avatar

Lardière Sébastien slardiere

View GitHub Profile
@apolloclark
apolloclark / postgres cheatsheet.md
Last active June 14, 2024 08:25
postgres cheatsheet

Postgres Cheatsheet

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.

Setup

installation, Ubuntu

http://www.postgresql.org/download/linux/ubuntu/ https://help.ubuntu.com/community/PostgreSQL

@chanmix51
chanmix51 / deduplicate.sql
Created May 29, 2014 09:52
Remove duplicate rows from a table
with
dup_id as (select id, count(*) from school group by id having count(*) > 1),
del_dup as (delete from only school using dup_id where school.id = dup_id.id returning school.*)
insert into school select distinct on (id) del_dup.* from del_dup order by del_dup.id returning *;