Freitag, 9. März 2012

Ruby and Postgresql or what you can do in database.yml (part 1)

Database connections in ruby are defined within the database.yml file. Unfortunately this is not really documented because each adapter has its own options to support the underlying database.

First thing you should not forget: ruby does use libpg - so any feature that is not provided by this library will not work within ruby. At the other hand: if you can connect with psql to your database, chances are good, that you can do it with ruby too.

Here is a list of options you can use in database.yml if you are using postgresql adapter:

  • host
    this is the hostname of the server which runs the postgresql instance
  • hostaddr
    this is the same as host but name resolve is not used, use this for raw ip addresses
  • port
    the port of the postgresql instance
  • dbname
    name of postgresql database
  • user
    username (don't forget to configure pg_hba.conf so that this user is allowed to connect)
  • password
    password for the username
  • sslmode
    specify the sslmode of the connection:
    • disable only use allow unencryted connections
    • allow use unencrypted first and if that fails try ssl
    • prefer first try ssl first but use unencrypted if ssl does not work
    • require use ssl or fail connection
  • connect_timeout
    maximum wait for the connection (in seconds), specify 0 to wait indefinitely
  • krbsrvname
    the service name of the corresponding kerberos
  • gsslib
    GSS library to use for GSS API
  • service
    service which references pg_service.conf

You always use <parameter> : <value> format

If you want to use a SSL encrypted connection, you should add sslmode:require to your database stanza in database.yml.