Skip to content

Instantly share code, notes, and snippets.

@GUIEEN
Forked from ibraheem4/postgres-brew.md
Last active July 29, 2019 08:26
Show Gist options
  • Save GUIEEN/87b1b5dfb0e2341061f074e71918819d to your computer and use it in GitHub Desktop.
Save GUIEEN/87b1b5dfb0e2341061f074e71918819d to your computer and use it in GitHub Desktop.
Installing Postgres via Brew (OSX)

Install GCC with Brew

$ brew install gcc

インストールはこれだけで終わりです。 Homebrew 経由で入れたパッケージは/usr/local/Celler/に保存され、/usr/local/bin/にコマンドのエイリアスが自動で貼られます。 /usr/local/bin/からエイリアスを探し当てます

$ ls /usr/local/bin | grep gcc
$ ls /usr/local/bin | grep g++

上記コマンドで見つかった gcc-8 と g++-8 がお目当てのエイリアスです。 見つかったエイリアスに対してシンボリックリンクを貼ってあげましょう。

$ ln -s /usr/local/bin/gcc-8 /usr/local/bin/gcc
$ ln -s /usr/local/bin/g++-8 /usr/local/bin/g++

最後に~/.bash_profile の良い感じの場所に以下の記述をして、優先順位高めのパスを通したら終了です。

export PATH=$PATH:/usr/local/bin

Installing Postgres via Brew

Pre-Reqs

Brew Package Manager

In your command-line run the following commands:

  1. brew doctor
  2. brew update

Installing

  1. In your command-line run the command: brew install postgresql

  2. Run the command: ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

  3. Create two new aliases to start and stop your postgres server. They could look something like this:

    alias pg_start="launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
    alias pg_stop="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
    
  4. Run the alias you just created: pg_start. Use this comment to start your database service.

    • alternatively, pg_stop stops your database service.
  5. Run the command: createdb `whoami`

  6. Connect to your postgres with the command: psql

  7. brew reinstall readline - if needed

  8. createuser -s postgres - fixes role "postgres" does not exist

  9. Test with psql command

    $ psql
    psql (10.0)
    Type "help" for help.
    
    ibraheem=# 
    

Details

What is this ln command I ran in my Terminal?

from the man ln command

The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the copies''; instead, a link points'' to the original copy. There are two types of links; hard links and symbolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link.

What is launchctl?

from the man launchctl command

launchctl interfaces with launchd to manage and inspect daemons, angents and XPC services.

Commands

Create database

createdb <database_name>

create database <database_name>

createdb mydjangoproject_development

List databases

psql -U postgres -l

\l

Show tables in database

psql -U postgres -d <database_name>

psql -U postgres -d mydjangoproject_development

Drop database

dropdb <database_name>

drop database <database_name>

dropdb mydjangoproject_development

if, database is accessed by other users, then

REVOKE CONNECT ON DATABASE TARGET_DB FROM public;

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB';

Restart database

dropdb <database_name> && createdb <database_name>

dropdb mydjangoproject_development && createdb mydjangoproject_development

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment