postgresqlをmacで使う
環境設定
brew install postgresql
起動
postgres -D /usr/local/var/postgres
2018-09-22 05:36:28.073 JST [41369] LOG: listening on IPv6 address "::1", port 5432 2018-09-22 05:36:28.073 JST [41369] LOG: listening on IPv4 address "127.0.0.1", port 5432 2018-09-22 05:36:28.075 JST [41369] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2018-09-22 05:36:28.099 JST [41370] LOG: database system was shut down at 2018-09-22 05:34:20 JST 2018-09-22 05:36:28.108 JST [41369] LOG: database system is ready to accept connections
ポートを指定したい場合
postgres -D /usr/local/var/postgres -p 54321
別のコンソールを開いて、DBにつなげる
psql -d postgres
psql (10.5) Type "help" for help. postgres=# postgres=# help You are using psql, the command-line interface to PostgreSQL. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit postgres=#
いろんな設定値を確認したい場合
postgres=# show all
表示を縦にしたいとき(もとに戻したいとき)
\x
テーブル一覧を確認したい場合。(事前に\connect
が必要)
<table_name>を入れなければすべて、入れると、そのテーブルの構造を表示する。
\d <table_name>
設定
create database XXX;
DBに接続するためのコマンド
Connection \c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo} connect to new database (currently "postgres") \conninfo display information about current connection \encoding [ENCODING] show or set client encoding \password [USERNAME] securely change the password for a user
\connect XXX
postgres=# \connect XXX; You are now connected to database "XXX" as user "ユーザ名".
ユーザ名は現在のユーザ名がデフォルトになっている。 ユーザ名を指定する場合は、connectコマンドのUSERに値を設定する。
ユーザ追加をする場合
CREATE USER UUU WITH PASSWORD 'pass';