{"id":2340,"date":"2025-03-09T15:22:13","date_gmt":"2025-03-09T08:22:13","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2340"},"modified":"2025-03-09T15:22:14","modified_gmt":"2025-03-09T08:22:14","slug":"postgresql-copy-database","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/","title":{"rendered":"PostgreSQL Copy Database"},"content":{"rendered":"\n<p><strong>Summary:<\/strong>&nbsp;In this tutorial, you&#8217;ll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.<\/p>\n\n\n\n<p>When working with PostgreSQL, you often need to copy or clone a database. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creating a backup of your database before making significant changes.<\/li>\n\n\n\n<li>Setting up a development or testing environment identical to production.<\/li>\n\n\n\n<li>Migrating data to a new server.<\/li>\n<\/ul>\n\n\n\n<p>Fortunately, PostgreSQL provides various ways to copy databases efficiently using built-in commands and tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"copying-database-within-the-same-postgresql-instance\" id='copying-database-within-the-same-postgresql-instance'>Copying Database Within the Same PostgreSQL Instance <a href=\"#copying-database-within-the-same-postgresql-instance\" class=\"anchor\" id=\"copying-database-within-the-same-postgresql-instance\" title=\"Anchor for Copying Database Within the Same PostgreSQL Instance\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL allows you to make a copy of an existing database using the&nbsp;<code>CREATE DATABASE<\/code>&nbsp;statement with the&nbsp;<code>TEMPLATE<\/code>&nbsp;option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> target_db\n<span class=\"hljs-keyword\">WITH<\/span> <span class=\"hljs-keyword\">TEMPLATE<\/span> source_db\n&#91;<span class=\"hljs-keyword\">OWNER<\/span> role_name];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Specify the new database&#8217;s name in the\u00a0<code>CREATE DATABASE<\/code>\u00a0clause.<\/li>\n\n\n\n<li>Provide the name of an existing database you want to copy in the\u00a0<code>WITH TEMPLATE<\/code>\u00a0clause.<\/li>\n\n\n\n<li>Assign the owner to the new database. If you don&#8217;t specify the owner, PostgreSQL will assign the role that runs the statement to the new database.<\/li>\n<\/ul>\n\n\n\n<p>This statement can copy (or clone) a database within the same PostgreSQL instance. Additionally, the source database must not have any active connections. <\/p>\n\n\n\n<p>We&#8217;ll show you step by step how to copy a database <code>dev<\/code> to <code>test<\/code> within the same PostgreSQL instance:<\/p>\n\n\n\n<p>First, connect to your PostgreSQL server using&nbsp;<code>psql<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">psql -U postgres<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-create-database\/\">create a new database<\/a> called\u00a0<code>dev<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> dev;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, switch the current database to&nbsp;<code>dev<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">\\c dev<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a>\u00a0<code>t<\/code>\u00a0inside the\u00a0<code>dev<\/code>\u00a0database and insert a new row into the\u00a0<code>t<\/code>\u00a0table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t(id <span class=\"hljs-type\">INT<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t(id) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-number\">1<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This step is to demonstrate that you can clone a database completely.<\/p>\n\n\n\n<p>Fifth, create a copy of the&nbsp;<code>dev<\/code>&nbsp;database to&nbsp;<code>test<\/code>&nbsp;database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> test\n<span class=\"hljs-keyword\">WITH<\/span> <span class=\"hljs-keyword\">TEMPLATE<\/span> dev;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the source database has any active connections, you can check using this query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> pid, usename, client_addr\n<span class=\"hljs-keyword\">FROM<\/span> pg_stat_activity\n<span class=\"hljs-keyword\">WHERE<\/span> datname =<span class=\"hljs-string\">'dev'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And close all connections to the source database using this query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> pg_terminate_backend(pid)\n<span class=\"hljs-keyword\">FROM<\/span> pg_stat_activity\n<span class=\"hljs-keyword\">WHERE<\/span> datname = <span class=\"hljs-string\">'dev'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, you can rerun the&nbsp;<code>CREATE DATABASE<\/code>&nbsp;statement to clone the&nbsp;<code>dev<\/code>&nbsp;database.<\/p>\n\n\n\n<p>Sixth, switch the current database to&nbsp;<code>test<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">\\c test<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, select data from the&nbsp;<code>t<\/code>&nbsp;table in the&nbsp;<code>test<\/code>&nbsp;database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> t;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id\n<span class=\"hljs-comment\">----<\/span>\n  <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"copying-a-database-using-pg_dump-and-pg_restore\" id='copying-a-database-using%c2%a0pg_dump%c2%a0and%c2%a0pg_restore'>Copying a Database Using\u00a0pg_dump\u00a0and\u00a0pg_restore <a href=\"#copying-a-database-using%c2%a0pg_dump%c2%a0and%c2%a0pg_restore\" class=\"anchor\" id=\"copying-a-database-using%c2%a0pg_dump%c2%a0and%c2%a0pg_restore\" title=\"Anchor for Copying a Database Using\u00a0pg_dump\u00a0and\u00a0pg_restore\">#<\/a><\/h2>\n\n\n\n<p>The most common way to copy a PostgreSQL database is by using&nbsp;<code>pg_dump<\/code>&nbsp;and&nbsp;<code>pg_restore<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pg_dump<\/code>\u00a0exports a database into a file.<\/li>\n\n\n\n<li><code>pg_restore<\/code>\u00a0restores a database from a dump file.<\/li>\n<\/ul>\n\n\n\n<p>This method works well for creating a database copy on the same or a different PostgreSQL server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-1-export-the-database-with-pg_dump\" id='step-1-export-the-database-with%c2%a0pg_dump'>Step 1: Export the Database with\u00a0pg_dump <a href=\"#step-1-export-the-database-with%c2%a0pg_dump\" class=\"anchor\" id=\"step-1-export-the-database-with%c2%a0pg_dump\" title=\"Anchor for Step 1: Export the Database with\u00a0pg_dump\">#<\/a><\/h3>\n\n\n\n<p>Run this command to export the&nbsp;<code>source_db<\/code>&nbsp;database into a file using a custom format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">pg_dump -U username -h host -p <span class=\"hljs-number\">5432<\/span> -F c -d source_db -f source_db.dump<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>-U username<\/code>\u00a0\u2013 PostgreSQL user.<\/li>\n\n\n\n<li><code>-h host<\/code>\u00a0\u2013 Database host.<\/li>\n\n\n\n<li><code>-p 5432<\/code>\u00a0\u2013 Port number.<\/li>\n\n\n\n<li><code>-F c<\/code>\u00a0\u2013 Custom format.<\/li>\n\n\n\n<li><code>-d source_db<\/code>\u00a0\u2013 Source database name.<\/li>\n\n\n\n<li><code>-f source_db.dump<\/code>\u00a0\u2013 Output dump file.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-2-create-a-new-database\" id='step-2-create-a-new-database'>Step 2: Create a New Database <a href=\"#step-2-create-a-new-database\" class=\"anchor\" id=\"step-2-create-a-new-database\" title=\"Anchor for Step 2: Create a New Database\">#<\/a><\/h3>\n\n\n\n<p>Create a new database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">psql -U username -h host -p <span class=\"hljs-number\">5432<\/span> -c \"CREATE DATABASE target_db;\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"step-3-restore-the-dump-to-the-new-database\" id='step-3-restore-the-dump-to-the-new-database'>Step 3: Restore the Dump to the New Database <a href=\"#step-3-restore-the-dump-to-the-new-database\" class=\"anchor\" id=\"step-3-restore-the-dump-to-the-new-database\" title=\"Anchor for Step 3: Restore the Dump to the New Database\">#<\/a><\/h3>\n\n\n\n<p>Run this command to restore the&nbsp;<code>source_db<\/code>&nbsp;backup into&nbsp;<code>target_db<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">pg_restore -U username -h localhost -p <span class=\"hljs-number\">5432<\/span> -d target_db -F c source_db.dump<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"copying-all-databases-using-pg_dumpall\" id='copying-all-databases-using%c2%a0pg_dumpall'>Copying All Databases Using\u00a0pg_dumpall <a href=\"#copying-all-databases-using%c2%a0pg_dumpall\" class=\"anchor\" id=\"copying-all-databases-using%c2%a0pg_dumpall\" title=\"Anchor for Copying All Databases Using\u00a0pg_dumpall\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you may copy all databases from a PostgreSQL instance, e.g., when you want to migrate the PostgreSQL server. In this case, you can use the&nbsp;<code>pg_dumpall<\/code>&nbsp;tool.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-1-export-all-databases-into-a-file\" id='step-1-export-all-databases-into-a-file'>Step 1: Export All Databases into a File <a href=\"#step-1-export-all-databases-into-a-file\" class=\"anchor\" id=\"step-1-export-all-databases-into-a-file\" title=\"Anchor for Step 1: Export All Databases into a File\">#<\/a><\/h3>\n\n\n\n<p>Run this command to export all databases on a PostgreSQL server to an SQL file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">pg_dumpall -U username -h host -p <span class=\"hljs-number\">5432<\/span> &gt; all_databases.<span class=\"hljs-keyword\">sql<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"step-2-import-all-databases-into-a-server\" id='step-2-import-all-databases-into-a-server'>Step 2: Import All Databases into a Server <a href=\"#step-2-import-all-databases-into-a-server\" class=\"anchor\" id=\"step-2-import-all-databases-into-a-server\" title=\"Anchor for Step 2: Import All Databases into a Server\">#<\/a><\/h3>\n\n\n\n<p>Run the following&nbsp;<code>psql<\/code>&nbsp;command to import all the databases into a server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">psql -U username -h host -p <span class=\"hljs-number\">5432<\/span> -f all_databases.<span class=\"hljs-keyword\">sql<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2340\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Copy Database\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2340\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Copy Database\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary:&nbsp;In this tutorial, you&#8217;ll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers. When working with PostgreSQL, you often need to copy or clone a database. For example: Fortunately, PostgreSQL provides various ways to copy databases efficiently using built-in commands and tools. Copying Database Within the Same PostgreSQL Instance # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2269,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2340","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Copy Database: A Step-by-Step Guide<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Copy Database: A Step-by-Step Guide\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-09T08:22:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/postgresql-copy-database\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/postgresql-copy-database\\\/\",\"name\":\"PostgreSQL Copy Database: A Step-by-Step Guide\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-03-09T08:22:13+00:00\",\"dateModified\":\"2025-03-09T08:22:14+00:00\",\"description\":\"In this tutorial, you'll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/postgresql-copy-database\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/postgresql-copy-database\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/postgresql-copy-database\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Database Administration\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-database-administration\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Copy Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL Copy Database: A Step-by-Step Guide","description":"In this tutorial, you'll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Copy Database: A Step-by-Step Guide","og_description":"In this tutorial, you'll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-03-09T08:22:14+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/","url":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/","name":"PostgreSQL Copy Database: A Step-by-Step Guide","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-03-09T08:22:13+00:00","dateModified":"2025-03-09T08:22:14+00:00","description":"In this tutorial, you'll learn how to copy a database within a PostgreSQL instance or between PostgreSQL servers.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/postgresql-copy-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Database Administration","item":"https:\/\/www.pgtutorial.com\/postgresql-database-administration\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Copy Database"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=2340"}],"version-history":[{"count":2,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2340\/revisions"}],"predecessor-version":[{"id":2343,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2340\/revisions\/2343"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2269"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}