Renaming a Database
SQLite has no direct SQL command to rename a database file.
You rename the file using your operating system, ideally when no process is using it.
Core Concepts
flowchart LR
A[Close DB connections] --> B[Rename file in OS]
B --> C[Reopen with new name]
C --> D[Verify with .databases]
| Rule | Reason |
|---|---|
| Close active SQLite sessions first | Avoid file locks/corruption risk |
| Rename at OS level | SQLite DB is a regular file |
| Reopen and verify | Prevent path confusion |
Code Examples
# Linux/macOS example: rename database file.
mv app_old.db app_new.db
| Expected output |
|---|
| No terminal output on success; file now has new name. |
# Windows PowerShell example: rename database file.
Rename-Item -Path "app_old.db" -NewName "app_new.db"
| Expected output |
|---|
| Usually no output on success; filename changes. |
SQLite-Specific Nuances
SQLite Nuance
Because SQLite databases are file-based, renaming is a filesystem operation, not a SQL DDL command.
Common Pitfalls / Best Practices
Pitfall
Renaming while the database is open in another process (CLI, app, or GUI), causing lock errors or inconsistent behavior.
Best Practice
After renaming, immediately open the new file and run .databases to confirm your session points to the renamed file.
Quick Challenge
Rename lab.db to lab_v2.db, open it, and verify main path.
View Solution
mv lab.db lab_v2.db
sqlite3 lab_v2.db
| Expected result |
|---|
.databases shows main attached to lab_v2.db. |