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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PostgreSQL

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

Ruby on Rails

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

Q&A

解決済

2回答

367閲覧

rails + mysqlがpsqlになる

退会済みユーザー

退会済みユーザー

総合スコア0

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PostgreSQL

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

Ruby on Rails

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

0グッド

1クリップ

投稿2019/06/04 02:14

OS: Ubuntu
OSバージョン: 18.04.2
rubyバージョン: 2.5.5
railsバージョン: Rails 5.2.3
mysqlバージョン: mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper

railsでmysqlを使うためにGemfileで以下のように記述し,

gem 'mysql2', '>= 0.4.4', '< 0.6.0'

そして,config/database.ymlを以下のように記述しました.

# MySQL. Versions 5.1.10 and up are supported. # # Install the MySQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default adapter: mysql2 encoding: utf8mb4 charset: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: username password: password socket: /tmp/mysql.sock development: <<: *default database: hoge_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: hoge_test # As with config/secrets.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix environment variable when you boot # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: hoge_production username: hoge password: <%= ENV['HOGE_APP_DATABASE_PASSWORD'] %>

そして,rails db:migrateをしたのですが,

rails aborted! LoadError: Error loading the 'postgresql' Active Record adapter. Missing a gem it depends on? pg is not part of the bundle. Add it to your Gemfile. /home/ubuntu/hoge_app/opinion_app/config/environment.rb:5:in `<top (required)>' /home/ubuntu/hoge_app/opinion_app/bin/rails:9:in `<top (required)>' /home/ubuntu/_app/opinion_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Gem::LoadError: pg is not part of the bundle. Add it to your Gemfile. /home/ubuntu/hoge_app/opinion_app/config/environment.rb:5:in `<top (required)>' /home/ubuntu/hoge_app/opinion_app/bin/rails:9:in `<top (required)>' /home/ubuntu/hoge_app/opinion_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate => db:load_config => environment (See full trace by running task with --trace)

となぜかpsqlになってしまっています.どうすればよいでしょうか?
(なお,セキュリティのため,ここに買いてあるDATABASE名やusernameは適当に変更しています)

##試したこと
MacOSでは同じプログラムでmysqlで動いたので,プログラムに問題はないと思います.

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

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

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

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

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

guest

回答2

0

ベストアンサー

ディレクトリを削除したりして,調べていたら,どうやらシェルの設定ファイル(私はfishなのでconfig.fish)にDATABASE_URLを書いていたのが原因でした.HOGE_URLなどに名前を変更し,rails dbするとmysqlに接続できました.

#fish shellの場合です #以下のようにしていたのが問題でした set -x DATABASE_URL 'postgres://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

投稿2019/06/04 16:56

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

Gemfileに記述した後に、

bundle install

は行いましたでしょうか。

投稿2019/06/04 03:38

fshun

総合スコア261

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

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

退会済みユーザー

退会済みユーザー

2019/06/04 04:06

はい,しています
fshun

2019/06/04 04:40

- mysql は起動していますでしょうか - mysql に database は作成されていますでしょうか `rails db:create`
退会済みユーザー

退会済みユーザー

2019/06/04 04:53

- `sudo mysql -u root`でmysqlへのアクセスが確認できましたので,起動しています. - `rails db:create`は以下のようにエラーが表示されできませんでした. ``` rails aborted! LoadError: Error loading the 'postgresql' Active Record adapter. Missing a gem it depends on? pg is not part of the bundle. Add it to your Gemfile. /home/ubuntu/twi_app/opinion_app/config/environment.rb:5:in `<top (required)>' /home/ubuntu/twi_app/opinion_app/bin/rails:9:in `<top (required)>' /home/ubuntu/twi_app/opinion_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Gem::LoadError: pg is not part of the bundle. Add it to your Gemfile. /home/ubuntu/hoge_app/opinion_app/config/environment.rb:5:in `<top (required)>' /home/ubuntu/hoge_app/opinion_app/bin/rails:9:in `<top (required)>' /home/ubuntu/hoge_app/opinion_app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:create => db:load_config => environment (See full trace by running task with --trace) ```
退会済みユーザー

退会済みユーザー

2019/06/04 04:54

なお,Gemfileに`gem 'pg'`とすると,`rails db`したときにpsqlに接続されてしまいます
fshun

2019/06/04 05:05

mysqlの起動確認ありがとうございます。 続けての質問、すみません。 Gemfileに `mysql2` 追加した際は、 `pg` は、削除されていますでしょうか。 また、Gemfileの中に `mysql2` を追加した後の、入力したコマンドなどを教えていただけないでしょうか。
退会済みユーザー

退会済みユーザー

2019/06/04 05:11

Gemfileの中身です ``` source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.3' # Use mysql as the database for Active Record gem 'mysql2', '>= 0.4.4', '< 0.6.0' #gem 'mysql2' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'omniauth' gem 'omniauth-twitter' gem 'bootstrap', '~> 4.3.1' gem 'jquery-rails' gem "attr_encrypted", "~> 3.1.0" #gem 'pg' ``` >>>Gemfileに `mysql2` 追加した際は、 `pg` は、削除されていますでしょうか。 コメントアウトして,Gemfile.lockを削除して`sudo bundle install`をしてみましたが,結果は変わりませんでした.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問