Here’s the procedure to rename a PostgreSQL database:
ALTER DATABASE
statement to rename the database to the new one.Let’s take a look at an example of renaming a database. We start by creating a database called old_db:
CREATE DATABASE old_db;
Then we’ll rename the old_db database to new_db following these steps:
First, disconnect from the database that you want to rename and connect to another database e.g., postgres. And ensure there’s no active connections to the target database to rename
\c postgres
-- Connect to the postgres database, so you are disconnected from the old_db database.
-- Next, check the all active connections to the old_db database by using the following query:
SELECT *
FROM pg_stat_activity
WHERE datname = 'old_db';
If the database you want to rename has many active connections, inform the respective users as well as the application owners before terminating the connections to avoid data loss.
If necessary, terminate all the remaining connections to the old_db database by using the following statement:
SELECT pg_terminate_backend (pid)
FROM pg_stat_activity
WHERE datname = 'db';
After that, rename the old_db database to new_db using the ALTER DATABASE RENAME TO
statement as follows:
ALTER DATABASE db RENAME TO newdb;
If you need to duplicate an existing Postgresql database, and possibly transfer ownership of the database objects to a new user, here’s how to do that in a quick way: -- First, I recommand getting the size of the database to copy, as this might be important for the rest of the process. SELECT pg_database.datname,pg_size_pretty(pg_database_size(... Read more
20 Apr 2019 - less than 1 minute readHere’s the way to force the shared pool to shrink dynamically. Documentation states that ASMM can only increase shared pool, and can’t shrink. When the automatic shared memory management feature is enabled, the internal tuning algorithm tries to determine an optimal size for the shared pool based on the workload. It usually converges on this va... Read more
20 Mar 2019 - 2 minute readIf you need to duplicate an existing Postgresql database, and possibly transfer ownership of the database objects to a new user, here’s how to do that in a quick way: -- First, I recommand getting the size of the database to copy, as this might be important for the rest of the process. SELECT pg_database.datname,pg_size_pretty(pg_database_size(... Read more
20 Apr 2019 - less than 1 minute readIn PostgreSQL, users can have many namespaces to resolve objects names. These are called schemas like in Oracle, and can be altered through the search_path variable. Here’s how to check current search path: SHOW search_path; /* Result search_path ------------------ "$user", public */ Usually it defaults to the username and public. So when yo... Read more
20 Jan 2018 - less than 1 minute read