parm530
5/18/2019 - 3:14 PM

PSQL

Check for POSTGRESQL

  • check for latest version: postgres --version
    • if command not found: brew install postgres
  • To have launchd start postgresql now and restart at login: brew services start postgresql
    • Or, if you don't want/need a background service you can just run: pg_ctl -D /usr/local/var/postgres start
  • To view other version of psql: brew search postgresql
    • To install a version: brew install postgresql@#
  • Make sure you run either start commands and not both!
  • If you run the pg_ctl command and you get an error about the user database not installed
    • run (within the service): createdb 'whoami': with tick marks, not single quotes!
  • Check to see if it runs: psql
    • psql utility function that lets you perform admin functions

For a clean install

  • brew doctor
  • brew update
  • brew uninstall postgres
  • rm -rf rm -rf /usr/local/var/postgres
  • rm -rf .psql_history .psqlrc .psql.local .pgpass .psqlrc.local
  • brew list to confirm postgres is actually deleted
  • brew update
  • brew install postgres

Rails with PSQL

  • MAKE SURE TO brew install postgresql BEFORE BUNDLING IF YOU HAVEN'T INSTALLED POSTGRESQL YET!!
  • rails new app_name -T -d postgresql

Configurations

  • Default user is postgres
  • Enter the console by: psql postgres
  • See all the users: \du
  • If missing the postgres user because you installed Postgres via homebrew (in terminal): /usr/local/opt/postgres/bin/createuser -s postgres
  • Set up password: psql -U postgres
    • \password postgres
    • follow prompts

Create new User

  • log in as postgres user: psql -U postgres OR psql postgres
    • With a user: psql postgres -U nameofuser
  • create a database: CREATE DATABASE name; don't forget the semicolon!!!*
  • create a user: CREATE ROLE nameofuser WITH PASSWORD '';
  • Grant priviliges: GRANT ALL PRIVILEGES ON DATABASE nameofDB TO nameofuser;
  • Grant only create db privilege: ALTER ROLE name CREATEDB;
  • \q quits
  • \list or \l lists all databases
  • \dt lists all tables in the database
  • \du lists all users
  • \dx lists all extensions used in the db
  • Link to documentation on how to postgres