dwyl / learn-postgresql

🐘 Learn how to use PostgreSQL and Structured Query Language (SQL) to store and query your relational data. 🔍
212 stars 23 forks source link

[SOLVED] How to rename PostgreSQL database using psql CLI? #80

Closed nelsonic closed 2 years ago

nelsonic commented 2 years ago

At present I have a Postgres database on my localhost that I don't want to DROP, but I want to start again from scratch for https://github.com/dwyl/auth/issues/174 ... So I want to rename the auth_dev database so I can re-use that name. 💭

This should be a 1 minute task. But I want to record it somewhere in case I need to do it again.

nelsonic commented 2 years ago

Indeed this was 1 min of googling to refresh memory ... The ALTER command is what we're looking for: https://www.postgresql.org/docs/current/sql-alterdatabase.html

In a terminal window on Mac/Linux type: psql to start the Postgres CLI. image

List the databases: \l (that's a backslash and lowercase "L"):

image

Rename:

The command to alter the name of the database is:

ALTER DATABASE db RENAME TO newdb;

In my case it was:

ALTER DATABASE auth_dev RENAME TO auth_dev_archive;

image

Looks like it worked:

image

Useful/relevant tutorial: