wordpress-heroku icon indicating copy to clipboard operation
wordpress-heroku copied to clipboard

Error establishing a database connection

Open catspaw opened this issue 12 years ago • 16 comments

Whenever I visit my site for the first time in the morning, I get: Error establishing a database connection

I receive the same error until I refresh 3-4 times, and then the site works as long as I keep hitting it.

It seems like something isn't running / needs to boot up / etc. but I have no idea how to configure the heroku/wordpress combo to keep the database running.

catspaw avatar Jan 03 '13 22:01 catspaw

I'm seeing same thing :(

ryw avatar Jan 04 '13 21:01 ryw

Try explicitly defining your DB host and port in wp-config. On line 31, replace

$db["host"]

with

'youramazonec2url.com:port'

which you can find by logging into Heroku Postgres. For me, line 31 looks now:

define('DB_HOST', 'ec2-54-243-238-144.compute-1.amazonaws.com:5432');

I was also experiencing this issue, but this fix seems to working well for me (touch wood!).

joshuawalker avatar Jan 04 '13 21:01 joshuawalker

Thanks for your reply, joshuawalker.

I've made the changes and will wait a few hours to see if when everything goes to "sleep" again, it continues to work.

catspaw avatar Jan 04 '13 22:01 catspaw

No problems since! Awesome -- thanks.

catspaw avatar Jan 05 '13 15:01 catspaw

FYI I replaced line 18 w/ full URL, which you can get from running heroku config

$db = parse_url('postgres://user:[email protected]:5432/xxx');

No idea why this works tho :) PHP loses access to ENV variable?

ryw avatar Jan 05 '13 18:01 ryw

Thanks for that, @ryw - seems like a more reliable fix.

joshuawalker avatar Jan 06 '13 06:01 joshuawalker

Problem occurred again this morning, despite the fix above. :/

catspaw avatar Jan 07 '13 16:01 catspaw

Interesting that mine was down too when I hit the URL, and came back a minute later...

ryw avatar Jan 07 '13 16:01 ryw

Seems like the PHP app needs a way to automatically reconnect like how in Rails database.yml one can set reconnect: true and the app magically works "better".

ryw avatar Jan 07 '13 19:01 ryw

Also experienced the issues again. Just tinkering, I changed the pg4wp plugin driver file to open a persistent, always open connection to the DB: on line 90, pg_connect to pg_pconnect. I don't actually know if this does or will do anything, but it functions at the moment...

Should be said that pconnect isn't suitable for heavy-load websites: http://www.manticmoo.com/articles/jeff/programming/postgresql/difference-between-pg_connect-and-pg_pconnect.php

joshuawalker avatar Jan 07 '13 23:01 joshuawalker

Thanks for idea @joshuawalker — just implemented that on my attempt, so far so good.

ryw avatar Jan 08 '13 19:01 ryw

Haven't experienced any connection issues thus far - will report back here if they come back.

joshuawalker avatar Jan 09 '13 04:01 joshuawalker

Database errors again this morning. :(

catspaw avatar Jan 10 '13 17:01 catspaw

An I right in assuming that this somehow has to do with Heroku spinning down the apache process when there's no active connections to the site for a while?

markrickert avatar Jan 10 '13 17:01 markrickert

@catspaw Have you tried pg_pconnect? I haven't experienced any connection issues since changing it.

joshuawalker avatar Jan 10 '13 20:01 joshuawalker

My wordpress site has been running on heroku for longer than a year...

Now I have tried to migrate my heroku wordpress to a simple hosting php+postgresql and I get this error all the time "Error establishing a database connection".

Any clues or help to fix it? :)

I solved in the end... I had a typo error in my wp-config.php. If anybody interested I set it like so:

// ** Heroku Postgres settings - from Heroku Environment ** // //$db = parse_url($_ENV["DATABASE_URL"]);

// ** MySQL settings - You can get this info from your web host * // /* The name of the database for WordPress */ define('DB_NAME', "dbname");

/** MySQL database username */ define('DB_USER', "dbuser");

/** MySQL database password */ define('DB_PASSWORD', "dbuserpassword");

/** MySQL hostname */ define('DB_HOST', "localhost");

/** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', '');

gugol2 avatar Sep 29 '14 17:09 gugol2