Solutions to fix the 'psql' is not recognized error on Windows by adding PostgreSQL to PATH or reinstalling PostgreSQL with psql.
| Student Notes | Database Management Systems (DBMS) |
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.
ALTER DATABASE is an SQL command used to modify the properties of an existing database.
We can use this command to:
In this lesson, we will learn how to rename a database.
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. |
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 DATABASE tells PostgreSQL that we want to modify a database.college is the current database name.RENAME TO specifies the renaming operation.university is the new database name.Expected Result
ALTER DATABASE
PostgreSQL displays this message when the database is successfully renamed.
PostgreSQL has some important restrictions when renaming a database.
Important: Always connect to another database, such as postgres, before renaming your target database.
We can also rename a database using pgAdmin.
Follow these steps:
postgresql - How restore a Heroku PG database on a MAC - Stack Overflow
How to Rename Databases in PostgreSQL — CommandPrompt Inc.
college to university.Note: If the database has active connections, close those connections before saving the new name.
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.
| 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 |
Exercise 1: Write SQL Commands
Write SQL commands to perform the following tasks:
school to college.college to university.student_db to students.library to central_library.Exercise 2: Practical Activity
Perform these tasks using pgAdmin:
my_college.postgres database.my_college to my_university.ALTER DATABASE?| 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.