# How to start/stop database from rake?

**URL:** <https://www.ruby-forum.com/t/how-to-start-stop-database-from-rake/182932>\
**Category:** Rails\
**Created:** [February 4, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/how-to-start-stop-database-from-rake/182932 "2010-02-04T22:42:01Z")\
**Posts on this page:** 1\
**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:** [February 4, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/how-to-start-stop-database-from-rake/182932/1 "2010-02-04T22:42:01Z")

</div>

Hello,

I would like to start the database server (postgres) before tests are  
run  
and stop it afterwards. For this, I have created a little wrapper around  
rake:

$ cat run-rake  
#! /bin/sh

DB=`pwd`/db/pgdata

# init database cluster if it does not exist yet

[-d $DB] || initdb -D$DB

# start the database server

pg\_ctl -D$DB -o “-h ‘’ -k $DB” -l postgreslog start

# wait for it to accept connections

sleep 1

# run rake with original arguments

rake “$\*”

# we’re done, stop database server again

pg\_ctl -D$DB -o “-h ‘’ -k $DB” -l postgreslog stop

But this doesn’t “feel” like RoR, So I’d like to integrate it into rake,  
so  
I can say “rake foobar” instead of “run-rake foobar”

Any hints?
