LoginSignup
20
20

More than 5 years have passed since last update.

Rails 4.2.6 database.ymlで指定できるオプション

Last updated at Posted at 2016-08-11

PostgreSQLでrake db:createするときのLC_CTYPELC_COLLATEを指定したいと思い、database.ymlの仕様を調べたが公式のドキュメントが見つからなかった。

どのデータベースでもよく使うオプション

この辺はご存知と思う。

adapter
host
port
database
username
password
pool
timeout

PostgreSQL

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html より

:host - Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets, the default is to connect to localhost.
:port - Defaults to 5432.
:username - Defaults to be the same as the operating system name of the user running the application.
:password - Password to be used if the server demands password authentication.
:database - Defaults to be the same as the user name.
:schema_search_path - An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the :schema_order option.
:encoding - An optional client encoding that is used in a SET client_encoding TO <encoding> call on the connection.
:min_messages - An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
:variables - An optional hash of additional parameters that will be used in SET SESSION key = val calls on the connection.
:insert_returning - An optional boolean to control the use of RETURNING for INSERT statements defaults to true.

Any further options are used as connection parameters to libpq. See www.postgresql.org/docs/current/static/libpq-connect.html for the list of parameters.

とあるので、ここに書いてあるパラメータは全部渡せると思われる。

さらにrake db:createのときに下記の指定が有効。

owner 
template
encoding
collation
ctype 
tablespace
connection_limit

どうやって調べたか:

activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/schema_statements.rb
        def create_database(name, options = {})
          options = { encoding: 'utf8' }.merge!(options.symbolize_keys)

          option_string = options.inject("") do |memo, (key, value)|
            memo += case key
            when :owner
              " OWNER = \"#{value}\""
            when :template
              " TEMPLATE = \"#{value}\""
            when :encoding
              " ENCODING = '#{value}'"
            when :collation
              " LC_COLLATE = '#{value}'"
            when :ctype
              " LC_CTYPE = '#{value}'"
            when :tablespace
              " TABLESPACE = \"#{value}\""
            when :connection_limit
              " CONNECTION LIMIT = #{value}"
            else
              ""
            end
          end

          execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
        end

SQLite

http://railsdoc.com/config によると

adapter 接続するデータベースの種類 sqlite3
database    データベースファイルまでのパス   db/環境名.sqlite3
pool    接続のブール数   5
timeout タイムアウト時間    5000

MySQL

http://railsdoc.com/config によると

adapter 接続するデータベースの種類 mysql2
database    接続先のデータベース名   db/アプリケーション名_環境名
host    接続先のサーバ名またはIPアドレス。socketを指定した場合には無効   localhost
post    接続先のポート番号。socketを指定した場合には無効   3306
socket  Unixソケットのパス   /tmp/mysql.sock
username    データベースに接続するユーザ名   root
password    DBに接続するパスワード    
encoding    文字エンコーディングを明示的に指定 utf8
pool    接続のブール数   5
timeout タイムアウト時間    false

さらにrake db:createのときに下記が有効らしい:

charset
collation
activerecord-4.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
      def create_database(name, options = {}) 
        if options[:collation]
          execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
        else
          execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
        end 
      end
20
20
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
20
20