Very useful article just saved me from a day of frustration.
When redeploying an organisation database from a foreign environment, the app pool will not have permission to the new database and must be configured in the SQL database's security settings.