--- /etc/init.d/postgresql	2013-08-02 04:50:43.000000000 -0400
+++ postgresql	2014-05-29 17:33:51.598667808 -0400
@@ -28,6 +28,16 @@
 
 case "$1" in
     start|stop|restart|reload)
+        # we need to create the /var/run/postgresql directory as root before
+        # dropping privileges so that the postgres user can start the database
+        # later, even in the event of internal server crash at boot. this
+        # directory needs to exist for postgres to run, and things will fail to
+        # start if it doesn't exist (see BUG #8177).
+        if [ -d /var/run/postgresql ]; then
+            chmod 2775 /var/run/postgresql
+        else
+            install -d -m 2775 -o postgres -g postgres /var/run/postgresql
+        fi
 	if [ -z "`pg_lsclusters -h`" ]; then
 	    log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
 	    exit 0
