-sudo -u postgres createuser -s vagrant
-sudo -u vagrant -H createdb -E UTF-8 -O vagrant openstreetmap
-sudo -u vagrant -H createdb -E UTF-8 -O vagrant osm_test
-# add btree_gist extension
-sudo -u vagrant -H psql -c "create extension btree_gist" openstreetmap
-# TODO: build and set up postgres extensions
+db_user_exists=`sudo -u postgres psql postgres -tAc "select 1 from pg_roles where rolname='vagrant'"`
+if [ "$db_user_exists" != "1" ]; then
+ sudo -u postgres createuser -s vagrant
+ sudo -u vagrant -H createdb -E UTF-8 -O vagrant openstreetmap
+ sudo -u vagrant -H createdb -E UTF-8 -O vagrant osm_test
+ # add btree_gist extension
+ sudo -u vagrant -H psql -c "create extension btree_gist" openstreetmap
+ sudo -u vagrant -H psql -c "create extension btree_gist" osm_test
+fi
+# build and set up postgres extensions
+pushd db/functions
+sudo -u vagrant make
+sudo -u vagrant psql openstreetmap -c "drop function if exists maptile_for_point(int8, int8, int4)"
+sudo -u vagrant psql openstreetmap -c "CREATE FUNCTION maptile_for_point(int8, int8, int4) RETURNS int4 AS '/srv/openstreetmap-website/db/functions/libpgosm.so', 'maptile_for_point' LANGUAGE C STRICT"
+popd