0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

rails sでPostgreSQLが実行できないエラー

Posted at

1.現状

ローカルでアプリケーションを実行しようとしたところ、以下のエラーがでました。

ActiveRecord::ConnectionNotEstablished (connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

翻訳
ActiveRecord::ConnectionNotsteaded (ソケット「/tmp/.s.PGSQL.5432」上のサーバーへの接続に失敗しました: そのようなファイルまたはディレクトリはありません)
サーバーはローカルで実行されており、そのソケットでの接続を受け入れていますか?

2.直近で行った変更

AWS-s3の導入を行いました。

3.エラー文から考えられる原因

postgresqlが立ち上がっていないことが原因と予測しました。

4.解決のために行ったこと

1.データベースを再起動する ✗

brew services restart postgresql

2.postmaster.pidを削除する ✗

何らかの原因でpostmaster.pidファイルが残ったままになりそれが原因でエラーが発生することがあるらしいのでpostmaster.pidの削除を行いました。

rm /usr/local/var/postgres/postmaster.pid

結果はpostmaster.pidは存在しない様子

rm: /usr/local/var/postgres/postmaster.pid: No such file or directory

3.PostgreSQLの再インストール ✗

#PostgreSQLのアンインストール
brew uninstall --force postgresql

#PostgreSQLの残ファイルを削除
rm -rf /usr/local/var/postgres

#PostgreSQLのインストール
brew install postgresql

4.権限付与 これで解決

/tmp🔒 
❯ postgres -D /usr/local/var/postgresql

2023-07-17 21:39:39.907 JST [71050] LOG:  starting PostgreSQL 15.3 on x86_64-apple-darwin20.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
2023-07-17 21:39:39.908 JST [71050] LOG:  listening on IPv6 address "::1", port 5432
2023-07-17 21:39:39.908 JST [71050] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-07-17 21:39:39.916 JST [71050] FATAL:  could not create lock file "/tmp/.s.PGSQL.5432.lock": Permission denied
2023-07-17 21:39:39.917 JST [71050] LOG:  database system is shut down

FATAL: could not create lock file "/tmp/.s.PGSQL.5432.lock": Permission denied
上記のような表示が出力され、tmpディレクトリにpermission denied(アクセス拒否)が発生していることがわかった。
そのため、以下のコマンドで許可を与えました。

$ sudo chmod 1777 /tmp 

以下コマンドで再確認してみます。

❯ pg_ctl -D /usr/local/var/postgresql start 

waiting for server to start....2023-07-17 21:55:46.188 JST [71852] LOG:  starting PostgreSQL 15.3 on x86_64-apple-darwin20.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
2023-07-17 21:55:46.190 JST [71852] LOG:  listening on IPv6 address "::1", port 5432
2023-07-17 21:55:46.190 JST [71852] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2023-07-17 21:55:46.191 JST [71852] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-07-17 21:55:46.199 JST [71855] LOG:  database system was shut down at 2023-07-17 21:47:31 JST
2023-07-17 21:55:46.212 JST [71852] LOG:  database system is ready to accept connections
 done
server started

ポスグレが動くか確認してみます。

❯ psql postgres
psql (15.3)
Type "help" for help.

postgres=#

ポスグレに入れるようになったがエラーは継続
エラーが少し変わりました。

connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist

role名が違うと言ってるみたいなので以下コマンドで確認

❯ psql postgres     
psql (15.3)
Type "help" for help.

postgres=# \du
                                    List of roles
 Role name  |                         Attributes                         | Member of 
------------+------------------------------------------------------------+-----------
 ロール名 | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \q

detabase.ymlに記載の環境変数username: <%= ENV['USERNAMEPG'] %>の中身を上記で確認したロール名に変更。
.envにて修正します。

以下コマンドでデータベース作成。

rails db:create
rails db:migrate

無事、エラーが解消されアプリケーションへ接続できました。

参考記事

PostgreSQLに接続できなかった時の対処法
PostgreSQL接続時に「psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed:」エラーが出た時の解消

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?