Fix 'psql' is not recognized on Windows
Solutions to fix the 'psql' is not recognized error on Windows by adding PostgreSQL to PATH or reinstalling PostgreSQL with psql.
Renaming a Database in PostgreSQL
Student Notes | Database Management Systems (DBMS)
1. Introduction
Sometimes, we create a database with an incorrect name or want to change its name to something more meaningful.
PostgreSQL provides the ALTER DATABASE command to rename an existing database.
Example:
Suppose we have created a database named:
college
Now, we want to change its name to:
university
We can rename the database without creating a new one.
2. What is ALTER DATABASE?
ALTER DATABASE is an SQL command used to modify the properties of an existing database.
We can use this command to:
- Rename a database.
- Change the database owner.
- Modify certain database settings.
In this lesson, we will learn how to rename a database.
3. SQL Syntax
The general syntax for renaming a database is:
ALTER DATABASE old_database_name
RENAME TO new_database_name;
Explanation:
| Part | Description |
|---|---|
ALTER DATABASE |
Specifies the database we want to modify. |
old_database_name |
Current name of the database. |
RENAME TO |
Specifies that we want to change its name. |
new_database_name |
New name of the database. |
4. Practical Example
Suppose we have an existing database named college.
We want to rename it to university.
SQL Command:
ALTER DATABASE college
RENAME TO university;
Explanation:
ALTER DATABASEtells PostgreSQL that we want to modify a database.collegeis the current database name.RENAME TOspecifies the renaming operation.universityis the new database name.
Expected Result
ALTER DATABASE
PostgreSQL displays this message when the database is successfully renamed.
5. Important Conditions Before Renaming
PostgreSQL has some important restrictions when renaming a database.
- You must have permission to rename the database.
- You cannot rename the database to a name that already exists.
- You cannot rename the database while connected to that same database.
- Other active connections to the database must be closed before renaming it.
Important: Always connect to another database, such as postgres, before renaming your target database.
6. Rename a Database Using pgAdmin
We can also rename a database using pgAdmin.
Follow these steps:
postgresql - How restore a Heroku PG database on a MAC - Stack Overflow
- Open pgAdmin.
- Connect to your PostgreSQL server.
- Expand the Databases section.
- Right-click the database you want to rename.
- Select Properties.
How to Rename Databases in PostgreSQL — CommandPrompt Inc.
- Open the General tab.
- Change the database name from
collegetouniversity. - Click Save.
Note: If the database has active connections, close those connections before saving the new name.
7. Rename a Database Using SQL in pgAdmin
We can also perform this task through the Query Tool.
Step 1: Connect to another database
For example, select the postgres database in pgAdmin and open its Query Tool.
Step 2: Execute the following command
ALTER DATABASE college
RENAME TO university;
Step 3: Verify the result
Execute:
SELECT datname
FROM pg_database;
The database list should now contain university instead of college.
8. Common Errors
| Error | Reason | Solution |
|---|---|---|
| Database is being accessed by other users | Active connections exist | Close other connections |
| Cannot rename the currently open database | Connected to the target database | Connect to postgres first |
| Database already exists | New name is already in use | Choose another name |
| Permission denied | Insufficient privileges | Use an authorized database owner or administrator |
9. Practice Exercises
Exercise 1: Write SQL Commands
Write SQL commands to perform the following tasks:
- Rename
schooltocollege. - Rename
collegetouniversity. - Rename
student_dbtostudents. - Rename
librarytocentral_library.
Exercise 2: Practical Activity
Perform these tasks using pgAdmin:
- Create a database named
my_college. - Connect to the
postgresdatabase. - Rename
my_collegetomy_university. - Verify the new database name.
- Open the database list and confirm that the old name no longer exists.
10. Review Questions
- Which SQL command is used to rename a database?
- What is the purpose of
ALTER DATABASE? - Write the general syntax for renaming a database.
- Can we rename a database while connected to it?
- Why should we close active connections before renaming?
- How can we verify that a database has been renamed?
- What happens if the new database name already exists?
11. Quick Revision
| Task | SQL Command |
|---|---|
| Create database | CREATE DATABASE college; |
| Rename database | ALTER DATABASE college RENAME TO university; |
| List databases | SELECT datname FROM pg_database; |
Key Takeaway: The ALTER DATABASE ... RENAME TO command is used to change the name of an existing PostgreSQL database. Always connect to a different database, such as postgres, before performing the renaming operation.