Skip to content

Instantly share code, notes, and snippets.

@ncole458
Last active December 14, 2017 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncole458/f4c36a05088278a92bb90f1f0639d62a to your computer and use it in GitHub Desktop.
Save ncole458/f4c36a05088278a92bb90f1f0639d62a to your computer and use it in GitHub Desktop.
CLI commands I seem to use sometimes & forget sometimes...
# connect to db
sudo -u postgres psql postgres
# export table as CSV
COPY table_name TO '/tmp/users_export.csv' DELIMITER ',' CSV HEADER;
# create db
CREATE DATABASE <name>;
# create role
CREATE ROLE <username> WITH LOGIN ENCRYPTED PASSWORD '<password>' CREATEDB;
# change bool or other
ALTER TABLE <table> ALTER <column> TYPE boolean USING (<column>::boolean);
# change smallint to bool
ALTER TABLE <table> ALTER <column> TYPE boolean USING (<column>::int::boolean);
# alter role
"/Applications/Postgres.app/Contents/Versions/9.6/bin/psql" -p5432 -d "database" "user"
ALTER USER myuser WITH SUPERUSER;
# drop/create schema
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
GRANT ALL ON SCHEMA public TO anyuser;
# reset pk/id sequence
SELECT pg_catalog.setval(pg_get_serial_sequence('table_name', 'id'), (SELECT MAX(id) FROM table_name)+1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment