initdb: error: directory "/usr/local/var/postgres" exists but is not empty

## 概要

[zauberware/rails-devise-graphql](https://github.com/zauberware/rails-devise-graphql) を使おうとして初めてmacにpostgresを入れたログです。

## 環境

* macOS Mojave v10.14.6
* pg_ctl (PostgreSQL) 13.1

## 現象

```sh
$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "gremito".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: error: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
```

## 解決

```sh
$ rm -fr /usr/local/var/postgres

$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "gremito".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

creating directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start
```

## 確認

```sh
$ pg_ctl -D /usr/local/var/postgres -l logfile start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.

$ brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

$ pg_ctl -D /usr/local/var/postgres -l logfile start
waiting for server to start.... done
server started
```

### `rake db`

`config/database.yml`設定後に以下確認

```sh
$ rake db:create
Created database 'rails_devise_graphql_development'
Created database 'db/rails_devise_graphql_test'

$ rake db:migrate
== 20190209162712 EnableExtensionForUuid: migrating ===========================
-- extension_enabled?("pgcrypto")
   -> 0.0556s
-- enable_extension("pgcrypto")
   -> 0.0236s
== 20190209162712 EnableExtensionForUuid: migrated (0.0793s) ==================

== 20190209163712 DeviseCreateUsers: migrating ================================
-- create_table(:users, {:id=>:uuid})
   -> 0.0188s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0084s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0050s
-- add_index(:users, :confirmation_token, {:unique=>true})
   -> 0.0036s
-- add_index(:users, :unlock_token, {:unique=>true})
   -> 0.0039s
-- add_index("users", ["refresh_token"], {:name=>"index_users_on_refresh_token", :unique=>true, :using=>:btree})
   -> 0.0051s
== 20190209163712 DeviseCreateUsers: migrated (0.0451s) =======================

== 20200911092211 CreateActiveStorageTables: migrating ========================
-- create_table(:active_storage_blobs)
   -> 0.0176s
-- create_table(:active_storage_attachments)
   -> 0.0232s
== 20200911092211 CreateActiveStorageTables: migrated (0.0410s) ===============

== 20200912120337 CreateCompanies: migrating ==================================
-- create_table(:companies, {:id=>:uuid})
   -> 0.0105s
== 20200912120337 CreateCompanies: migrated (0.0106s) =========================

== 20200912153138 AddSlugToCompanies: migrating ===============================
-- add_column(:companies, :slug, :string)
   -> 0.0027s
-- add_index(:companies, :slug, {:unique=>true})
   -> 0.0036s
== 20200912153138 AddSlugToCompanies: migrated (0.0065s) ======================

== 20200912153858 CreateFriendlyIdSlugs: migrating ============================
-- create_table(:friendly_id_slugs)
   -> 0.0084s
-- add_index(:friendly_id_slugs, :sluggable_id)
   -> 0.0056s
-- add_index(:friendly_id_slugs, [:slug, :sluggable_type])
   -> 0.0060s
-- add_index(:friendly_id_slugs, [:slug, :sluggable_type, :scope], {:unique=>true})
   -> 0.0036s
-- add_index(:friendly_id_slugs, :sluggable_type)
   -> 0.0034s
== 20200912153858 CreateFriendlyIdSlugs: migrated (0.0359s) ===================

== 20200920102035 DeviseInvitableAddToUsers: migrating ========================
-- change_table(:users)
   -> 0.0442s
== 20200920102035 DeviseInvitableAddToUsers: migrated (0.0443s) ===============

Annotated (3): app/models/user.rb, spec/models/user_spec.rb, spec/factories/users.rb

$ rake db:seed

```

　<img width="500" alt="スクリーンショット 2020-12-03 12.54.33.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82601/5ba4ac65-1fc4-73f7-cabf-ce1b5cf117d4.png">

[PSequel](https://akiyoko.hatenablog.jp/entry/2016/07/29/230931) にて正常にDBとテーブルが作られたので終わり。

## 参考記事

- [macOSにPostgreSQLをインストール](https://kentama.hatenablog.com/entry/2017/04/23/222335)
- [MacOS CatalinaでPostgresqlをインストール・初期操作](https://qiita.com/kouki-T/items/d6444b81ce65d6412146)