Attatching a Content DB to a Web Application

For each Web Application you create in SharePoint, by default, there is a single Content DB that is assigned to it. Typically, the steps for creating a web application are as follows:

  1. Launch your Central Administration web site.
  2. On the Quick Launch bar to the left of the page, click on Application Management.
  3. Under the SharePoint Web Application Management heading, click on Create or extend Web application.
  4. Click on Create a new Web Applicaiton.
  5. The Create New Web Application page requires some information before creating your new web application. I won’t go through all of this in detail, but let’s pay particular attention to the Database Name and Authentication section. I have attempted in the past to alter the database name to match the already existing content db i would like this web application to use. This is not recommended nor does it work consistently. What you’ll want to do is accept all defaults for the database name and proceed with creating your web application. In the next steps we’ll outline how you swap this content db out for the one your already existing content db.

Now once your content db has been created, you’ll need to go back to the Application Management screen. From there, try the following:

  1. Click on Content databases.
  2. On the right of the toolbar, look for the Web Application drop down. Make sure you change this to match the web app you’re trying to replace the content db for. I find that sometimes this is defaulted to the Central Admin web application which is not what we want.
  3. Click on the database name link, this should bring up the Manage Content Database Settings page. Change your Database status to Offline and check Remove content database. Click OK. This content db shouldn’t contain anything since we just created this web application.
  4. We should now be redirected back to the Manage Content Databases page. Click on Add a content database.
  5. The only thing you need to change on this page is the Database Name field. Set this to the name of the content db you want to add. Set the search server and click OK.

Now typically this is a smooth operation, however, if you encounter the error below:

Attaching this database requires an upgrade, which could time out the browser session. You must use the STSADM command ‘addcontentdb’ to attach this database.

Open up a command prompt window, and type the following stsadm command in:

stsadm -o addcontentdb -url http://server01/ -databasename WSS_Content

There are other arguments you can use for this command, to see a list of those, type:

stsadm -o addcontentdb

Once the command has completed successfully, try loading your site. If you have any issues at all with this, drop me a line.

13 Responses to “Attatching a Content DB to a Web Application”

  1. Vinit July 22, 2011 at 9:32 pm #

    After running addcontentdb command i am getting error that
    “Upgrade completed with errors: review the upgrade.log file”. i am writing some last lines of upgrade.log file. Please help.

    [SPContentDatabaseSequence] [DEBUG] [7/23/2011 10:30:07 AM]: Calling get SchemaVersion on Database Navigate_ContentDB, Status = Upgrading.
    [SPContentDatabaseSequence] [DEBUG] [7/23/2011 10:30:07 AM]: Executing database object restore script.
    [SPUtility] [DEBUG] [7/23/2011 10:30:07 AM]: File C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12Templatesqlstoreup.sql, Time out = 9848 sec
    [SPManager] [ERROR] [7/23/2011 10:30:14 AM]: Upgrade [SPContentDatabase Name=Navigate_ContentDB Parent=SPDatabaseServiceInstance] failed.
    [SPManager] [ERROR] [7/23/2011 10:30:14 AM]: A severe error occurred on the current command. The results, if any, should be discarded.
    The statement has been terminated.
    [SPManager] [ERROR] [7/23/2011 10:30:14 AM]: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at Microsoft.SharePoint.Utilities.SqlSession.ExecuteScript(TextReader textReader, Int32 commandTimeout)
    at Microsoft.SharePoint.Utilities.SqlSession.ExecuteScript(String path, Int32 commandTimeout)
    at Microsoft.SharePoint.Upgrade.SPUtility.ExecuteSqlFile(SqlSession sqlSession, ISqlSession isqlSession, SqlFile sqlFileId, Int32 timeOut)
    at Microsoft.SharePoint.Upgrade.SPUtility.ExecuteSqlFile(SqlSession sqlSession, SqlFile sqlFileId, Int32 timeOut)
    at Microsoft.SharePoint.Upgrade.SPContentDatabaseSequence.Upgrade()
    at Microsoft.SharePoint.Upgrade.SPManager.Upgrade(Object o, Boolean bRecurse)
    [SPManager] [DEBUG] [7/23/2011 10:30:14 AM]: Elapsed time upgrading [SPContentDatabase Name=Navigate_ContentDB Parent=SPDatabaseServiceInstance]: 00:00:08.
    [SPManager] [INFO] [7/23/2011 10:30:14 AM]: Resetting the status of PersistedUpgradableObject: SPContentDatabase Name=Navigate_ContentDB Parent=SPDatabaseServiceInstance to Online.
    [SPManager] [DEBUG] [7/23/2011 10:30:14 AM]: Using cached [SPContentDatabase Name=Navigate_ContentDB Parent=SPDatabaseServiceInstance] NeedsUpgrade value: True.
    [SPManager] [DEBUG] [7/23/2011 10:30:14 AM]: Using cached [SPContentDatabase Name=WSS_Content_adfb8f46eeab4c04a266d14a0aa88ac3 Parent=SPDatabaseServiceInstance] NeedsUpgrade value: False.
    [SPManager] [INFO] [7/23/2011 10:30:14 AM]: Inplace Upgrade session finishes. root object = SPContentDatabase Name=Navigate_ContentDB Parent=SPDatabaseServiceInstance, recursive = False. 1 errors and 0 warnings encountered.

  2. shereen July 22, 2011 at 10:25 pm #

    can you email me the entire log file if it’s not too big? shereen at qumsieh dot ca.

  3. bhargava May 22, 2012 at 1:59 am #

    Hi shereen I did this process but I am unable to browse my site ….
    Please help me@

  4. shereen May 25, 2012 at 3:41 pm #

    just sent you an email.

  5. Fabian January 17, 2013 at 8:04 am #

    Hi, hpe you can help, I’ve followed your instructions and everithing seems to work fine, the problem is that when I try to look for the sites that come with the restored db nothing shows up.


  6. shereen January 17, 2013 at 9:40 am #

    Hi Fabian,

    What version of SharePoint did you try this in?

  7. Mark January 27, 2013 at 11:44 am #

    I have been tasked with moving our DB’s from one SQl Server to another, I was able to get Sharepoint 2007 to see the Config DB and removed and re-added the content DB’s to the web apps, all sites are up and running but I am unsure on how to add the admin content DB to the CA site, can I just remove and re-add it like the others or will it need to be added through command line?


  8. shereen January 27, 2013 at 2:04 pm #

    Hi Mark, I’ve never done that to be honest, I usually recreate central admin in the destination and redo the config/admin settings. Is that an option for you?

  9. Mark Lilly August 26, 2013 at 1:01 am #

    I have tried the above method but getting the generic “Error Occurred” message.
    What I am trying to do is lift a sharepoint environment from one domain and bring it into another. Should the approach you describe work for this scenario? (There is a trust relationship in place between the domains.)

  10. Shereen Qumsieh September 6, 2013 at 5:49 pm #

    Hi Mark,

    What version of SharePoint are you doing this with? What do the source and target environments look like?

  11. Javier Leyton December 12, 2013 at 11:34 pm #

    Hi, i tried this and it attached perfectly but it doesn’t show any sites from the DB, if i query the table webs from the content DB i see all of them, but i can’t load them, any help please?

  12. Shane June 17, 2015 at 5:42 pm #

    I have one site collection per database. I created a new database then a new site collection and saw that the new site collection was added to the new database. I then use the stsadm deletecontendb command to delete the database (i guess that’s the same as doing it in the UI) then i use the addcontendb command to add the one i have restored from production.

    I receive this error: Databases must be empty before they can be used

    Any ideas? I saved and restored the database by saving it to SQL create scripts. I used those scripts to restore it, along with data, on the new site. This is the only way i can do it, because i need to clear out list items, but i need everything else and can not use site collection backup and restore (its over 50gb).


    Please help.


  13. Mark February 5, 2016 at 4:24 am #

    Great article, covered everything very concisely. Thank you Shereen.

Leave a Reply