Setting tempdb as Default Database for New SQL Server Logins

 

When creating new SQL Server logins, it's a best practice to set tempdb as the default database instead of leaving it as master (the system default).

Why This Matters

Prevents accidental pollution of system databases: Users who execute CREATE statements without explicitly specifying a database context will create objects in their default database. If the default is master, this can clutter the critical system database with user objects, potentially causing maintenance issues and confusion.

Safer environment for learning and testing: For users learning SQL Server or testing scripts, tempdb provides a sandboxed environment. Since tempdb is recreated on every SQL Server restart, any test tables, procedures, or other objects are automatically cleaned up.

Reduces deployment risks: Automated deployment scripts that don't explicitly specify database context will default to tempdb, preventing accidental modifications to production databases or master.

- Create login with tempdb as default database 

CREATE LOGIN [LoginName] WITH PASSWORD = 'SecurePassword123!', 

  DEFAULT_DATABASE = tempdb; 

-- Modify existing login ALTER LOGIN [ExistingLogin] 

WITH DEFAULT_DATABASE = tempdb;


Key Benefits

  • Automatic cleanup of test objects on server restart
  • Protection of master and production databases from accidental object creation
  • Safer environment for ad-hoc queries and script testing
  • Reduced risk during automated deployments

This simple configuration change significantly reduces the risk of database pollution while providing a more forgiving environment for learning and development work.

Comments