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

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

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

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Heroku

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

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

4回答

4869閲覧

http://localhost:3000でエラーメッセージ「PG::ConnectionBad」が表示されてしまう

namitop

総合スコア29

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Heroku

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

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

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

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

1グッド

0クリップ

投稿2016/02/11 19:07

編集2016/02/12 05:58

ドットインストール Heroku入門「#05 Railsアプリケーションを作ろう」(http://dotinstall.com/lessons/basic_heroku/28805)をしていて起きた問題です。

このレッスンは
vagrant上のcentos6.7でrailsを動かすもので、
データベースはpostgreSQLを利用しています。

「rails s」してブラウザで確認したら以下のようなエラーメッセージが出ました。
なぜmymemo_developmentデータベースが作成されないのかわかりません。
また、「http://localhost:3000」を表示するだけなのにも関わらずデータベースが存在しないのが問題になるのかがわかりません。
どこが原因でしょうか?

以下に「エラーメッセージ」「mymemo/Gemfile」「mymemo/config/database.yml」の内容を載せます。

【エラーメッセージ】


PG::ConnectionBad
FATAL: データベース"mymemo_development"は存在しません

Rails.root: /home/vagrant/mymemo

Application Trace | Framework Trace | Full Trace
Request

Parameters:

None
Toggle session dump
Toggle env dump
Response

Headers:

None


【mymemo/Gemfile】

vi

1source 'https://rubygems.org' 2 3 4# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 5gem 'rails', '4.1.4' 6# Use postgresql as the database for Active Record 7gem 'pg' 8# Use SCSS for stylesheets 9gem 'sass-rails', '~> 4.0.3' 10# Use Uglifier as compressor for JavaScript assets 11gem 'uglifier', '>= 1.3.0' 12# Use CoffeeScript for .js.coffee assets and views 13gem 'coffee-rails', '~> 4.0.0' 14# See https://github.com/sstephenson/execjs#readme for more supported runtimes 15gem 'therubyracer', platforms: :ruby 16 17# Use jquery as the JavaScript library 18gem 'jquery-rails' 19# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 20gem 'turbolinks' 21# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 22gem 'jbuilder', '~> 2.0' 23# bundle exec rake doc:rails generates the API under doc/api. 24gem 'sdoc', '~> 0.4.0', group: :doc 25 26# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 27gem 'spring', group: :development 28 29# Use ActiveModel has_secure_password 30# gem 'bcrypt', '~> 3.1.7' 31 32# Use unicorn as the app server 33# gem 'unicorn' 34 35# Use Capistrano for deployment 36# gem 'capistrano-rails', group: :development 37 38# Use debugger 39# gem 'debugger', group: [:development, :test] 40

【mymemo/config/database.yml】

vi

1# PostgreSQL. Versions 8.2 and up are supported. 2# 3# Install the pg driver: 4# gem install pg 5# On OS X with Homebrew: 6# gem install pg -- --with-pg-config=/usr/local/bin/pg_config 7# On OS X with MacPorts: 8# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 9# On Windows: 10# gem install pg 11# Choose the win32 build. 12# Install PostgreSQL and put its /bin directory on your path. 13# 14# Configure Using Gemfile 15# gem 'pg' 16# 17default: &default 18 adapter: postgresql 19 encoding: unicode 20 # For details on connection pooling, see rails configuration guide 21 # http://guides.rubyonrails.org/configuring.html#database-pooling 22 pool: 5 23 24development: 25 <<: *default 26 database: mymemo_development 27 28 # The specified database role being used to connect to postgres. 29 # To create additional roles in postgres see `$ createuser --help`. 30 # When left blank, postgres will use the default role. This is 31 # the same name as the operating system user that initialized the database. 32 #username: mymemo 33 34 # The password associated with the postgres role (username). 35 #password: 36 37 # Connect on a TCP socket. Omitted by default since the client uses a 38 # domain socket that doesn't need configuration. Windows does not have 39 # domain sockets, so uncomment these lines. 40 #host: localhost 41 42 # The TCP port the server listens on. Defaults to 5432. 43 # If your server runs on a different port number, change accordingly. 44 #port: 5432 45 46 # Schema search path. The server defaults to $user,public 47 #schema_search_path: myapp,sharedapp,public 48 49 # Minimum log levels, in increasing order: 50 # debug5, debug4, debug3, debug2, debug1, 51 # log, notice, warning, error, fatal, and panic 52 # Defaults to warning. 53 #min_messages: notice 54 55# Warning: The database defined as "test" will be erased and 56# re-generated from your development database when you run "rake". 57# Do not set this db to the same as development or production. 58test: 59 <<: *default 60 database: mymemo_test 61 62# As with config/secrets.yml, you never want to store sensitive information, 63# like your database password, in your source code. If your source code is 64# ever seen by anyone, they now have access to your database. 65# 66# Instead, provide the password as a unix environment variable when you boot 67# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 68# for a full rundown on how to provide these environment variables in a 69# production deployment. 70# 71# On Heroku and other platform providers, you may have a full connection URL 72# available as an environment variable. For example: 73# 74# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 75# 76# You can use this database configuration with: 77# 78# production: 79# url: <%= ENV['DATABASE_URL'] %> 80# 81production: 82 <<: *default 83 database: mymemo_production 84 username: mymemo 85 password: <%= ENV['MYMEMO_DATABASE_PASSWORD'] %> 86
hato_pato👍を押しています

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

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

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

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

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

guest

回答4

0

mymemo_developmentが作成されていないように見受けられますので、
多分手順はこちらを参考されると良いかと。
http://qiita.com/cigalecigales/items/a12a780d300cca0844bd

投稿2016/02/15 04:52

takataka1984

総合スコア66

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

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

namitop

2016/02/17 19:06

URLの「config/database.ymlのdevelopmentを以下のように修正。」というところで、usernameをpostgresにしていると思うのですが、これって言い方が分からないですが”良い”のですか? Railsアプリケーションにスーパーユーザーを渡してもいいのでしょうか?
takataka1984

2016/02/18 02:00

スーパーユーザ利用について まずは動く事を目的とした場合には、利用されたほうが先に進みやすいと思います。 またこの記事は購読者が確実にあるユーザで利用したほうが話が進みやすいのでデフォルトユーザを利用していると思われます。 一般的なお話をしますと、 基本的に利用しないほうがいいです。 なぜならばコードにハードコーディングされている場合で、リポジトリが想定以外の人に閲覧可能なときに悪さができるからです。 そのため実際に稼働する場合にはユーザを作成の上、実施して頂いたほうが良いと思います。
guest

0

GemFileのコメントは削除してますか?

rake DB:migrate
は実施してますか?

投稿2016/02/12 04:50

hato_pato

総合スコア215

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

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

namitop

2016/02/12 05:12

「rake DB:migrate 」はまだしていません。 ただ、このドットインストールの動画で言うと0:55の、 ブラウザに「192.168.78.90:3000」と入力して表示するときにこの質問のエラーメッセージが表示されました。 この時でも、データベースが存在してなければ、Railsの初期画面である「Welcome aboard」が表示されないのでしょうか? ちなみに、 rails new mymemo -d postgresql と入力してRailsアプリケーションを作成しました。 あと関係があるかわからないですが、 sudo service postgresql restartは実行できず、 sudo service postgresql-9.4 restartは実行できました。
hato_pato

2016/02/12 05:23 編集

rails new mymemo -d postgresql cd mymemo vi Gemfile コメント削除 rails s の順で実施していますか? おそらくですが、rails new mymemo -d postgresqlで DBは作成されていると思います。 Gemfileの修正(1行コメントを削除)して rails sでサーバを起動しているので、ここで起動できれば Welcome画面がブラウザで見れると思います。
namitop

2016/02/12 05:34

rails sのとき、コメントの部分が影響するんですか? 「gem 'therubyracer', platforms: :ruby」の前についていた「*」は消しました。 以下にGemfile内を載せます。 ```vi source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.4' # Use postgresql as the database for Active Record gem 'pg' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.3' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring', group: :development # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Use debugger # gem 'debugger', group: [:development, :test] ```
namitop

2016/02/12 06:03

「Gemfileの修正(1行コメントを削除)」は Gemfile内の「#」から始まる全てを削除するという意味でしょうか?
namitop

2016/02/12 06:10

rake db:migrateを実行すると以下のようになりました。 [vagrant@localhost mymemo]$ rake db:migrate rake aborted! PG::ConnectionBad: FATAL: データベース"mymemo_development"は存在しません Tasks: TOP => db:migrate (See full trace by running task with --trace)
hato_pato

2016/02/12 06:10

書き方が悪かったです。 #だけを消すということです。
namitop

2016/02/12 06:17

以下のようなものの「#gem~」の「#」を消して、すべて有効にするということでしょうか? # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Use debugger # gem 'debugger', group: [:development, :test]
guest

0

pg_hba.confを修正しlocalhost指定の接続を
可能にすることで解決しないでしょうか。

参考ページ

投稿2016/02/12 08:32

hato_pato

総合スコア215

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

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

namitop

2016/02/12 09:32

参考ページの「pg_hba.confを修正しlocalhost指定の接続を可能にする」を行いました。 「/var/lib/pgsql/data/pg_hba.conf」はなかったので、「/var/lib/pgsql/9.4/data/pg_hba.conf」でしたが、 このファイルの「IPv4 local connections:」のident を trustに変更して、「sudo service postgresql-9.4 restart」しました。 ただ変わらず、以下のようなエラーメッセージが起きました。 んー、"ユーザ名指定だけで無条件に接続する"と書かれてるのに。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーー PG::ConnectionBad FATAL: ユーザ"vagrant"のIdent認証に失敗しました Rails.root: /home/vagrant/mymemo ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
hato_pato

2016/02/12 09:43 編集

IPv6という事はないですよね? vagrantでcreatedb mymemo_developmentで DB作成はできます?
namitop

2016/02/17 18:56

>vagrantでcreatedb mymemo_developmentでDB作成はできます? vagrantユーザーでDB作成ができるかどうかですよね?su vagrantしてdb作成できました。duして権限を確認したのですが、vagrant・mymemoロールにDB作成の権限があるみたいです。 postgres=> \du ロール一覧 ロール名 | 属性 | メンバー ----------+--------------------+---------- mymemo | DBを作成できる | {} postgres | スーパーユーザ | {} : ロールを作成できる : DBを作成できる vagrant | DBを作成できる | {}
namitop

2016/02/17 18:59

この記事(http://senoway.hatenablog.com/entry/2013/09/21/231706)の、 ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ ただ、注意しなければいけない点として、 ・OSのユーザ名とpostgresのユーザ名は同じでないと操作できない ・postgres内にそのユーザに属するDBが存在しないと操作できない ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ ってどういうことですか? ドットインストールのこのレッスンをやっていて意識していなかったところなので、自分の状況だとどのように置き換えられるのでしょうか?
hato_pato

2016/02/18 01:12

現状だと vagrantというOSユーザとpostgresのユーザがいるので1点目はクリア。 2点目は、ユーザがvagrantでアクセス(読み書き)権限がDB mymemo_developerに設定されていればOKという意味だと思います。 psql> \z で確認できるはずです。
guest

0

mymemo/config/database.yml

username: mymemo のコメント削除
host: localhost のコメント削除
port: 5432 のコメント削除
をして

rails s
した場合はどうなりますか?

投稿2016/02/12 06:31

hato_pato

総合スコア215

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

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

namitop

2016/02/12 06:36

hato_patoさんの上記のコメント通りrails sをすると、以下のようになりました。 エラーは起きたのですが、FATALの内容が変わりました。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー PG::ConnectionBad FATAL: ユーザ"mymemo"のIdent認証に失敗しました Rails.root: /home/vagrant/mymemo Application Trace | Framework Trace | Full Trace Request Parameters: None Toggle session dump Toggle env dump Response Headers: None ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
hato_pato

2016/02/12 06:55

DBは作成されたものと思います。 あとは作成したDBに接続可能なユーザを作成して rails sを実行すれば問題なくなりそうです。 こちらのサイトのcreateuser で検索されるとユーザ作成ができますので 試してみてください。 http://qiita.com/yyamnk/items/ad1c090653c824c9f7ad
namitop

2016/02/12 07:41

psql -lをしたところ、mymemo_developmentは作成されていませんでした。 貼っていただいたURLを参考に usernameとpasswordを設定し、 それに合わせてdatabase.ymlのところも書いたのですが、 変わらず以下のようなメッセージが起きました。なぜなのでしょうか? ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー PG::ConnectionBad FATAL: ユーザ"mymemo"のIdent認証に失敗しました Rails.root: /home/vagrant/mymemo Application Trace | Framework Trace | Full Trace Request Parameters: None Toggle session dump Toggle env dump Response Headers: None ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
namitop

2016/02/12 08:21

以下のようにロール一覧にmymemoがあるのですが、 「su - mymemo」をしようとしても「su: mymemo というユーザは存在しません 」となってしまいます。 ちなみに、「su - postgres」「su - vagrant」だとパスワードを要求されたので、 「su - vagrant」をしてmymemo/config/database.ymlのusernameとpasswordをvagrantのものにしたのですが、『PG::ConnectionBad FATAL: ユーザ"vagrant"のIdent認証に失敗しました』になりました。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー [vagrant@localhost mymemo]$ psql postgres psql (8.4.20, サーバ 9.4.5) 注意: psql バージョン 8.4, サーババージョン 9.4. psql の機能の中で、動作しないものがあるかもしれません。 "help" でヘルプを表示します. postgres=> \du ロール一覧 ロール名 | 属性 | メンバー ----------+--------------------+---------- mymemo | DBを作成できる | {} postgres | スーパーユーザ | {} : ロールを作成できる : DBを作成できる vagrant | DBを作成できる | {} postgres=> \q [vagrant@localhost mymemo]$ cd [vagrant@localhost ~]$ su - mymemo su: mymemo というユーザは存在しません ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問