Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Rename DB

Hello,

I have been trying to rename my DB in localhost:

ALTER DATABASE old_name  
Modify Name = new_name;

and it does not work. I wonder why?

Unrecognized alter operation. (near "" at position 0)

0 likes
7 replies
jlrdw's avatar

In whatever SQL designer you are using Create a new database and there should be a command to copy your old database schema with tables to the new database with the new name.

Don't forget to update your migrations.

davy_yg's avatar
Level 27

I only need the sql command. I wonder what is the correct sql syntax to make it works.

Tray2's avatar

You can't rename it just like that.

You need to export the data to a new database.

Export

mysqldump -u [username] -p [database_name] > [dumpfilename.sql]

Import

mysql -u [username] -p [database_name] < [dumpfilename.sql]

1 like
davy_yg's avatar
Level 27

Is it not possible to rename a database?

Do I have to export it -> create a new database ->import it -> delete the old one ?

davy_yg's avatar
Level 27

I think renaming the database it used to work. I forget when? I wonder why it does not work this time.

Cronix's avatar

For a brief time mysql had the ability to do that. They removed it because it was deemed to be dangerous and peoples data was becoming corrupt.

exporting the database, creating a new database, and importing the old data into the new db is a good option, and it will work without problems.

Another (possible) option that seems to work for innodb db/tables, is to

  1. create a new database, let's say "new_db"
  2. for each existing table, run RENAME TABLE old_db.table TO new_db.table; where old_db is your original database name and new_db is the new one, and table is obviously the individual table name

I haven't tried that, but it's supposed to be pretty damn fast.

Always backup your data before trying anything

Please or to participate in this conversation.