Skip to content

Commit

Permalink
Do not expect database user with app name to exist
Browse files Browse the repository at this point in the history
By default when creating a project with `--database=postgresql` the `config/database.yml` file that is generated has a user specified that is the same as the app name

```
development:
  adapter: postgresql
  encoding: unicode
  database: <%= app_name %>_development
  pool: 5
  username: <%= app_name %>
  password:
```

This is counterintuitive and would rarely be valid. By default postgres creates a user with the current user name (http://www.postgresql.org/docs/9.3/static/database-roles.html) "it will have the same name as the operating system user that initialized the database cluster":

```
$ whoami
schneems
```

If the `username` is left out postgresql will assume that you wish to log in as the default user

```
$ psql -c '\du'
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 schneems  | Superuser, Create role, Create DB, Replication | {}
```

A good sensible default then for auto generated `database.yml` files is to remove the `username`, and have postgres attempt to connect to the database as the currently logged in user.

Instead of submitting with a blank password, don't submit a password.
  • Loading branch information
schneems committed Dec 20, 2013
1 parent e4cde5d commit dece621
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
default: &default default: &default
adapter: postgresql adapter: postgresql
encoding: unicode encoding: unicode
username: <%= app_name %>
password:


development: development:
<<: *default <<: *default
database: <%= app_name %>_development database: <%= app_name %>_development


# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: <%= app_name %>

# The password associated with the postgres role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a # Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have # domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines. # domain sockets, so uncomment these lines.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ default: &default
# For details on connection pooling, see rails configuration guide # For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling # http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5 pool: 5
username: <%= app_name %>
password:


development: development:
<<: *default <<: *default
database: <%= app_name %>_development database: <%= app_name %>_development


# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: <%= app_name %>

# The password associated with the postgres role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a # Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have # domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines. # domain sockets, so uncomment these lines.
Expand Down

0 comments on commit dece621

Please sign in to comment.