# How to specify working directory in config/database.yml?

**URL:** <https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731>\
**Category:** Rails\
**Created:** [September 1, 2009, 11:56am UTC](https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731 "2009-09-01T11:56:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josef\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@Josef\_W](https://www.ruby-forum.com/u/Josef_W)\
**Post date:** [September 1, 2009, 11:56am UTC](https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731/1 "2009-09-01T11:56:15Z")

</div>

Hello folks,

I’d like to use postgresql with unix domain sockets for  
testinng/development  
to avoid mismatches when tests from multiple working copies are run  
concurrently.

I can almost get what I want with the following entry in  
config/database.yml:

test:  
adapter: postgresql  
encoding: unicode  
database: testapp\_test  
username:  
password:  
host: /home/ruby/rails/testapp/db/pgdata

Unfortunately, the path is specified absolutely here. So when I check  
out  
two working copies of my application and run the tests in parallel on  
both  
copies, the database will get mangled.

So I am looking for a possibility to specify the directory relatively.  
Maybe

```
host: `pwd`/db/pgdata

```

or

```
host: #{RAILS_DIR}/db/pgdata

```

or something.

Can this (or something similar) be done? Any suggestions?

---

<div class="post-metadata">

**Author:** ![Josef\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@Josef\_W](https://www.ruby-forum.com/u/Josef_W)\
**Post date:** [September 3, 2009, 12:02am UTC](https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731/2 "2009-09-03T00:02:05Z")

</div>

Josef W. wrote:

> Hello folks,
> 
> I’d like to use postgresql with unix domain sockets for  
> testinng/development  
> to avoid mismatches when tests from multiple working copies are run  
> concurrently.
> 
> I can almost get what I want with the following entry in  
> config/database.yml:
> 
> test:  
> adapter: postgresql  
> encoding: unicode  
> database: testapp\_test  
> username:  
> password:  
> host: /home/ruby/rails/testapp/db/pgdata
> 
> Unfortunately, the path is specified absolutely here. So when I check  
> out  
> two working copies of my application and run the tests in parallel on  
> both  
> copies, the database will get mangled.
> 
> So I am looking for a possibility to specify the directory relatively.  
> Maybe
> 
> ```
> host: `pwd`/db/pgdata
> 
> ```
> 
> or
> 
> ```
> host: #{RAILS_DIR}/db/pgdata
> 
> ```
> 
> or something.
> 
> Can this (or something similar) be done? Any suggestions?

Since config/database.yml is read through ERB, you can do stuff like

host: \<%= RAILS\_DIR %\>/db/pgdata

or

host: \<%= `hostname` %\>

Check vendor/rails/railties/lib/initializer.rb, this is from Rails 2.3:

```
# Loads and returns the contents of the 

```

#database\_configuration\_file. The  
# contents of the file are processed via ERB before being sent  
through  
# YAML::load.  
def database\_configuration  
require ‘erb’  
YAML::load(ERB.new(IO.read(database\_configuration\_file)).result)  
end

---

<div class="post-metadata">

**Author:** ![Josef\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@Josef\_W](https://www.ruby-forum.com/u/Josef_W)\
**Post date:** [September 3, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731/3 "2009-09-03T21:24:27Z")

</div>

Josef W. wrote:

> On Thu, Sep 03, 2009 at 12:02:05AM +0200, Stephan W. wrote:
> 
> > Josef W. wrote:  
> > Thanks for your help, Stephan!
> 
> > > So I am looking for a possibility to specify the directory relatively.  
> > > host: `pwd`/db/pgdata  
> > > or  
> > > host: #{RAILS\_DIR}/db/pgdata  
> > > or something.
> > 
> > Since config/database.yml is read through ERB, you can do stuff like
> > 
> > host: \<%= RAILS\_DIR %\>/db/pgdata
> 
> Yeah, this works fine when I use RAILS\_ROOT instead of RAILS\_DIR.
> 
> > host: \<%= `hostname` %\>

Try

> > host: \<%= `hostname`.chomp %\>

> Backticks don’t seem to work here. I get a syntax error when I try it:
> 
> \*\* Invoke db:create (first\_time)  
> \*\* Invoke environment (first\_time)  
> \*\* Execute environment  
> rake aborted!  
> syntax error on line 22, col 9: `# port: 5432' /usr/lib64/ruby/1.8/yaml.rb:133:in`load’  
> /usr/lib64/ruby/1.8/yaml.rb:133:in `load' /usr/lib64/ruby/gems/1.8/gems/rails-2.1.1/lib/initializer.rb:716:in **`database\_configuration’

I couldn’t reproduce this, but chomp should help

Stephan

> Where line 22 is the first line after the backtick that does _not_ start  
> with a comment.

---

<div class="post-metadata">

**Author:** ![Josef\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@Josef\_W](https://www.ruby-forum.com/u/Josef_W)\
**Post date:** [September 3, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/how-to-specify-working-directory-in-config-database-yml/174731/4 "2009-09-03T21:02:58Z")

</div>

On Thu, Sep 03, 2009 at 12:02:05AM +0200, Stephan W. wrote:

> Josef W. wrote:  
> Thanks for your help, Stephan!

> > So I am looking for a possibility to specify the directory relatively.  
> > host: `pwd`/db/pgdata  
> > or  
> > host: #{RAILS\_DIR}/db/pgdata  
> > or something.
> 
> Since config/database.yml is read through ERB, you can do stuff like
> 
> host: \<%= RAILS\_DIR %\>/db/pgdata

Yeah, this works fine when I use RAILS\_ROOT instead of RAILS\_DIR.

> host: \<%= `hostname` %\>

Backticks don’t seem to work here. I get a syntax error when I try it:

\*\* Invoke db:create (first\_time)  
\*\* Invoke environment (first\_time)  
\*\* Execute environment  
rake aborted!  
syntax error on line 22, col 9: `# port: 5432' /usr/lib64/ruby/1.8/yaml.rb:133:in`load’  
/usr/lib64/ruby/1.8/yaml.rb:133:in `load' /usr/lib64/ruby/gems/1.8/gems/rails-2.1.1/lib/initializer.rb:716:in **`database\_configuration’

Where line 22 is the first line after the backtick that does _not_ start  
with a comment.
