質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1816閲覧

RailsでPostgresを使う

Usama_bin_Ladin

総合スコア1

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/05/17 13:58

編集2020/05/17 14:06

概要

Railsのアプリを作りました。
Herokuに載せる前にまずはローカルでDBを「sqlite3」から「Postgresql」に変更したいと思っていますが上手くいきません。

環境

ruby 2.6.6
Rails 6.0.3
Windows 10

やったこと

①PostgreSQLのインストールを実施。下記フォルダが作成される。
「C:\Program Files\PostgreSQL\12」

②以下のようにGemfileの「gem 'sqlite3'」をコメントアウトし、「gem 'pg'」を追加し、「bundle install」を実行

Gemfile

1#gem 'sqlite3' 2gem 'pg'

③database.ymlを以下のように修正

databaseyml

1# SQLite version 3.x 2# gem install sqlite3 3# 4# Ensure the SQLite 3 gem is defined in your Gemfile 5# gem 'sqlite3' 6# 7default: &default 8 adapter: postgresql 9 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 10 timeout: 5000 11 12development: 13 <<: *default 14 database: appname_development 15 16# Warning: The database defined as "test" will be erased and 17# re-generated from your development database when you run "rake". 18# Do not set this db to the same as development or production. 19test: 20 <<: *default 21 database: appname_test 22 23production: 24 <<: *default 25 database: appname_production 26 username: appname 27 password: <%= ENV['APPNAME_DATABASE_PASSWORD'] %>

④PostgreSQL\12\data\pg_hba.conf」を以下のように修正
※「trust」の部分。「md5」⇒「trust」に変更。

# IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. host replication all 127.0.0.1/32 trust host replication all ::1/128 trust

#結果
上記の対応を行い、再実施したところ、以下のエラーが発生しました。

PG::ConnectionBad FATAL: ���[��"username"�͑��݂��܂��� Extracted source (around line #58): ### Convenience alias for PG::Connection.new. def self::connect( *args ) return PG::Connection.new( *args ) end pg-1.2.3-x64 (mingw32) lib/pg.rb:58:in `initialize' pg-1.2.3-x64 (mingw32) lib/pg.rb:58:in `new' pg-1.2.3-x64 (mingw32) lib/pg.rb:58:in `connect' activerecord (6.0.2.2) lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection' activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection' activerecord (6.0.2.2) lib/active_record/connection_handling.rb:238:in `retrieve_connection' activerecord (6.0.2.2) lib/active_record/connection_handling.rb:206:in `connection' activerecord (6.0.2.2) lib/active_record/migration.rb:562:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call' activesupport (6.0.2.2) lib/active_support/callbacks.rb:101:in `run_callbacks' actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call' web-console (4.0.2) lib/web_console/middleware.rb:132:in `call_app' web-console (4.0.2) lib/web_console/middleware.rb:28:in `block in call' web-console (4.0.2) lib/web_console/middleware.rb:17:in `catch' web-console (4.0.2) lib/web_console/middleware.rb:17:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' railties (6.0.2.2) lib/rails/rack/logger.rb:38:in `call_app' railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `block in call' activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `block in tagged' activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:28:in `tagged' activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `tagged' railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call' rack (2.2.2) lib/rack/method_override.rb:24:in `call' rack (2.2.2) lib/rack/runtime.rb:22:in `call' activesupport (6.0.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/static.rb:126:in `call' rack (2.2.2) lib/rack/sendfile.rb:110:in `call' actionpack (6.0.2.2) lib/action_dispatch/middleware/host_authorization.rb:83:in `call' railties (6.0.2.2) lib/rails/engine.rb:526:in `call' puma (4.3.3) lib/puma/configuration.rb:228:in `call' puma (4.3.3) lib/puma/server.rb:682:in `handle_request' puma (4.3.3) lib/puma/server.rb:472:in `process_client' puma (4.3.3) lib/puma/server.rb:328:in `block in run' puma (4.3.3) lib/puma/thread_pool.rb:134:in `block in spawn_thread'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

解決しました。
原因は以下2点です。

  • 1.database.ymlにユーザー情報を指定していない
  • 2.postgreSQLのDBサーバーにデータベースを作成していなかった

↓解決手順を記載します。↓

Ⅰ.まずエラーメッセージの解析

error

1FATAL: ���[��"username"�͑��݂��܂���

コマンドプロンプトから"psql -l"と入力しても反応がないため(なぜか環境変数が無視される)
「C:\Program Files\PostgreSQL\12\bin」に移動し、"psql -l"と入力。
すると下記エラーメッセージが出力。これでおそらく文字化けの部分が判明した。

psql: エラー: サーバに接続できませんでした: FATAL: データベース"username"は存在しません

"username"(実際はローマ字で私の名前)というデータベースが無いとのこと。
「やったこと - ②」 でデータベースの切り替えは完了したと思っていた。

Ⅱ.データベースを作る

下記サイトにてpostgresでユーザーの追加方法が記載してあった。
「postgres」という権限の強いユーザーがデフォルトで設定してあると判明。

■PostgreSQLでユーザー(ロール)を追加してテーブルにアクセスする
https://qiita.com/wb773/items/248e6e083b2fe12e820a

database.ymlの"default"に「username」「password」を追加

default: &default adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: postgres password: password timeout: 5000

※passwordは"password"に下記手順で再設定しました。
1.「psql -U postgres -d postgres」でpostgresロールでログイン
2.「alter role postgres with password 'password';」を実行

「rails db:create」を実行。database.ymlの設定でDBが作成される。

Ⅲ.テーブル定義の再設定

「rake db:migrate」で完了。

投稿2020/05/17 15:16

Usama_bin_Ladin

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問