Skip to content

Instantly share code, notes, and snippets.

View joaquinmoreira's full-sized avatar

Joaquín Moreira joaquinmoreira

View GitHub Profile
@joaquinmoreira
joaquinmoreira / close_postgresql_connections.sql
Created March 6, 2014 15:54
Close opened connections to postgresql
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, <username>; -- Prevent from accepting new connections
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid() -- Don't kill my own connection!
AND datname = <database>; -- Don't kill the connections to other databases
@joaquinmoreira
joaquinmoreira / export_select_postresql.sql
Last active August 29, 2015 13:57
Export rows from a select and import back in postgresql
COPY (SELECT * FROM sample_table) TO '/path/to/file';
COPY sample_table FROM '/path/to/file';
SELECT setval('sample_table_id_seq', max(id)) FROM sample_table; -- Update primary key sequence