はじめに
postgresqlをmacに導入するのに地味につまづいた点について記載していきます。
導入方法
homebrewを使って導入します。
$ brew update
$ brew install postgresql
インストールできているか確認します
$ cd /usr/local/bin
$ ./psql --version
psql (PostgreSQL) 10.5
導入できていることが確認できたら以下のようにしてPATHを通します。
vi ~/.bash_profile
「i」を押下してinsertモードに切り替えてから
export PATH=/usr/local/postgresql/10.5/bin/:$PATH
と追記をして、escを押して「:wq」で終了します。
起動とつまづいたエラー
起動
$ postgres -D ./postgres/
これで起動を行う.
つまづいたエラー1
$ postgres -D ./postgres/
postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
このようなエラーが出た場合はinitdbで作成したディレクトリを-D付きのコマンドライン引数か、
環境変数PGDATAで指定してから実行をする。
$ cd Documents
$ mkdir postgres
$ initdb ./postgres -E utf8
つまづいたエラー2
また起動時にいかのようなエラーでがてきた時には
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
rm -r /usr/local/var/postgres/postmaster.pid
でファイルを削除することで解決する。
つまづいたエラー3
以下のエラーが出てきた場合
2018-11-23 XXXXXXXXXXXX FATAL: data directory "/Users/hoge/Documents/postgres" has group or world access
2018-11-23 XXXXXXXXXXXX DETAIL: Permissions should be u=rwx (0700).
書いてある通りに以下を実行する
chmod -R 700 /Users/hoge/Documents/postgres
で解決できた。フルアクセスできないようにした。(らしい。よくわからないができた。)
改めて
$ postgres -D ./postgres/
で起動できたら別のターミナル画面から以下が実行できればOK!
$ psql -d postgres
psql (10.5)
Type "help" for help.
postgres=#
試しに
こちらのサイトからサンプルデータをダウンロードしてみました。
以下でパラメーターを設定しておいて
export PGHOST=localhost
export PGPORT=5432
export PGDATABASE=postgres
export PGUSER=hoge #ユーザー名を指定
load.commandファイルを実行するとターミナルが起動されて、以下が実行されます。
$ /Users/hoge/Downloads/stdsql-20150724/load.command ; exit;
CREATE TABLE
COPY 4
CREATE TABLE
COPY 5
CREATE TABLE
COPY 10
CREATE TABLE
COPY 5
CREATE TABLE
COPY 10
CREATE TABLE
COPY 6
CREATE TABLE
COPY 5
CREATE TABLE
COPY 5
CREATE TABLE
COPY 1
CREATE TABLE
COPY 1
CREATE TABLE
COPY 24
CREATE TABLE
COPY 4
CREATE TABLE
COPY 6
CREATE TABLE
COPY 3
CREATE TABLE
COPY 10
CREATE TABLE
COPY 1
CREATE TABLE
COPY 3
CREATE TABLE
COPY 4
OK [Enter]
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[プロセスが完了しました]
先ほど起動しておいたターミナルから以下を「\dt」を実行すると
取り込めていることがわかりました!
$ psql -d postgres
psql (10.5)
Type "help" for help.
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------------------+-------+--------
public | access_log | table | hiropy
public | access_log_dyn | table | hiropy
public | access_log_wide | table | hiropy
public | customer_category_mapping | table | hiropy
public | customer_locations | table | hiropy
public | customers | table | hiropy
public | items | table | hiropy
public | monthly_sales | table | hiropy
public | order_details | table | hiropy
public | order_details_h | table | hiropy
public | order_details_v | table | hiropy
public | orders | table | hiropy
public | pivot_table | table | hiropy
public | search_log_dyn | table | hiropy
public | shops | table | hiropy
public | web_pages | table | hiropy
public | yearly_orders | table | hiropy
public | yearly_sales | table | hiropy
(18 rows)
参照
MacでPATHを通す
Postgres could not connect to server
プログラム の個人的なメモ
Postgresqlに接続できなくなった時
【初心者向け】PostgreSQLの接続と切断を分かり易く解説
psql: FATAL: role “postgres” does not exist
macOs Sierra + homebrewな環境でPostgresqlを導入する備忘録