{"id":5183,"date":"2021-04-02T18:06:40","date_gmt":"2021-04-02T18:06:40","guid":{"rendered":"https:\/\/chemicloud.com\/kb\/?post_type=ht_kb&#038;p=5183"},"modified":"2022-04-04T11:12:39","modified_gmt":"2022-04-04T11:12:39","slug":"import-and-export-a-postgresql-database","status":"publish","type":"ht_kb","link":"https:\/\/chemicloud.com\/kb\/article\/import-and-export-a-postgresql-database\/","title":{"rendered":"How to Import and Export a PostgreSQL Database"},"content":{"rendered":"<p>From time to time you may need to export or import a PostgreSQL database. For example, you might be moving the database from one hosting provider to another, importing a database from a third party for repair or migrations, or you may be taking a backup of the database.<\/p>\n<p>This knowledgebase article covers the various ways to import and export a PostgreSQL database.<\/p>\n<h2 id=\"how-to-export-a-postgresql-database\">How to export a PostgreSQL database<\/h2>\n<h3 id=\"method-1-use-the-pg_dump-application\">Method #1 Use the pg_dump application<\/h3>\n<p>The pg_dump command will allow you to export a PostgreSQL database to a file. To export using this method, follow the steps below:<\/p>\n<p><strong>Step 1:<\/strong> Connect to your <a href=\"https:\/\/chemicloud.com\/kb\/article\/log-account-via-ssh\/\">web hosting account<\/a> via SSH. If you are not familiar with how to do this, click here to review our KB article on the topic.<\/p>\n<p><strong>Step 2:<\/strong> Enter the following command using your keyboard and at the end, press enter\/return on your keyboard. Be sure to replace the &#8216;username&#8217; with your cPanel default account username and replace &#8216;dbname&#8217; with the name of the database you wish to export.<\/p>\n<pre>pg_dump -U username dbname &gt; dbexport.pgsql<\/pre>\n<p>You will be prompted for your default cPanel user account password in order to complete the export.<\/p>\n    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Important Info:<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tYour database will be downloaded with the name dbexport.pgsql. You can change the name of the database to anything you want after saving the file, but don&#8217;t change the extension.\u00a0    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Important Information:<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tYou may receive an error message, similar to the ones below, when running this command.<\/p>\n<pre>pg_dump: SQL command failed \r\n\r\npg_dump: Error message from server: ERROR: permission denied for schema topology \r\n\r\npg_dump: The command was: LOCK TABLE topology.topology IN ACCESS SHARE MODE<\/pre>\n<p>These errors occur because some server database templates include PostGIS with restricted access permissions.<\/p>\n<p>To export a PostgreSQL database without this data, type the following command instead of the command listed in step 2:<\/p>\n<pre>pg_dump -U username dbname -N topology -T spatial_ref_sys &gt; dbexport.pgsql<\/pre>\n    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n<h3 id=\"method-2-use-phppgadmin\">Method #2 Use phpPGAdmin<\/h3>\n<p><strong>Step 1:<\/strong> Login to your cPanel. There&#8217;s a lot of ways to do this, but the sure fire easiest way is to login to your <a href=\"https:\/\/lab.chemicloud.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Client Area<\/a>, then open your cPanel.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4061\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab.jpg\" alt=\"\" width=\"1674\" height=\"496\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab.jpg 1674w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-300x89.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-1024x303.jpg 1024w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-768x228.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-50x15.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-1536x455.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-60x18.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-100x30.jpg 100w\" sizes=\"auto, (max-width: 1674px) 100vw, 1674px\" \/><\/p>\n<p><strong>Step 2:<\/strong> Scroll down to the Databases section and open phpPgAdmin.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5186\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-783x211.jpg\" alt=\"\" width=\"643\" height=\"173\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-783x211.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-300x81.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-768x207.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-50x13.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-60x16.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-100x27.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1.jpg 1330w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p><strong>Step 3:<\/strong> When phpPgAdmin opens, expand the Servers area from the left hand side, then expand PostgreSQL, and then click the name of the database you wish to export.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5187\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-783x177.jpg\" alt=\"\" width=\"643\" height=\"145\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-783x177.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-300x68.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-768x174.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-50x11.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-1536x348.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-60x14.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2-100x23.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/2.jpg 1917w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p><strong>Step 4:<\/strong> From the menu at the top, click the Export button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5188\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-783x167.jpg\" alt=\"\" width=\"643\" height=\"137\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-783x167.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-300x64.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-768x163.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-50x11.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-1536x327.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-60x13.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3-100x21.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/3.jpg 1918w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>Under the Format column, select Structure and data. Under the Options column, in the Format list box, select SQL.<\/p>\n<p>Then beneath Options, select Download.<\/p>\n<p>Finally, click Export.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5190\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-783x248.jpg\" alt=\"\" width=\"643\" height=\"204\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-783x248.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-300x95.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-768x243.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-50x16.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-1536x487.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-60x19.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1-100x32.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/4-1.jpg 1918w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>The file will be saved to your computer.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"p1\"><b>Tired of experiencing issues with your site? Get the best and fastest hosting support with ChemiCloud!\u00a0<\/b><span class=\"s1\">\ud83e\udd13<\/span><b> Check out our <\/b><a href=\"https:\/\/chemicloud.com\/pricing#60b65e4e63b58\"><span class=\"s2\"><b><span style=\"text-decoration: underline;\">web hosting<\/span> <\/b><\/span><\/a><b>plans!<\/b><\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"how-to-import-a-postgresql-database\">How to import a PostgreSQL database<\/h2>\n<p>Before you can import a PostgreSQL database, you must create a new database in cPanel and assign a user to it.<\/p>\n    \t\t<div class=\"hts-messages hts-messages--info  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Pro Tip:<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tYou should import all of your PostgreSQL databases as the primary PostgreSQL user, aka the username assigned to your cPanel account. If you import PostgreSQL databases as a regular user, you will be unable to see or manipulate the data properly using phpPgAdmin.<\/p>\n<p>After you have imported the data as the primary PostgreSQL user, you can grant a regular user access to the database. Then you won&#8217;t need to use the primary domain username and password in scripts that access the database.     \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n<h3 id=\"method-1-use-the-psql-application\">Method # 1: Use the psql application<\/h3>\n<p><strong>Step 1:<\/strong> Using SCP, SFTP, FTP, or the cPanel File Manager &gt; Upload File tool, upload the database you want to import to your hosting account.<\/p>\n<p><strong>Step 2:<\/strong> Connect to your <a href=\"https:\/\/chemicloud.com\/kb\/article\/log-account-via-ssh\/\">web hosting account<\/a> via SSH. If you are not familiar with how to do this, click here to review our KB article on the topic.<\/p>\n<p>Step 3: Enter the following command, then press enter\/return on your keyboard. Replace &#8216;username&#8217; with your cPanel username and replace &#8216;dbname&#8217; with the name of the database you wish to import:<\/p>\n<pre>psql -U username dbname &lt; dbexport.pgsql<\/pre>\n<p>The &#8216;dbname&#8217; database should now contain all the data that was in the database file you uploaded.<\/p>\n<h3 id=\"method-2-use-phppgadmin-2\">Method # 2: Use phpPgAdmin<\/h3>\n<p>To import a PostgreSQL database using phpPgAdmin, follow the steps below:<\/p>\n<p><strong>Step 1:<\/strong> Login to your cPanel. There&#8217;s a lot of ways to do this, but the sure fire easiest way is to login to your <a href=\"https:\/\/lab.chemicloud.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Client Area<\/a>, then open your cPanel.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4061\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab.jpg\" alt=\"\" width=\"1674\" height=\"496\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab.jpg 1674w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-300x89.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-1024x303.jpg 1024w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-768x228.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-50x15.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-1536x455.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-60x18.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2020\/12\/lab-100x30.jpg 100w\" sizes=\"auto, (max-width: 1674px) 100vw, 1674px\" \/><\/p>\n<p><strong>Step 2:<\/strong> Scroll down to the Databases section and open phpPgAdmin.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5186\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-783x211.jpg\" alt=\"\" width=\"643\" height=\"173\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-783x211.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-300x81.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-768x207.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-50x13.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-60x16.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1-100x27.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/1.jpg 1330w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>Step 3: From the left side of phpPgAdmin, expand the Servers area, then expand PostgreSQL, then click the name of the database you want to import data into.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5191\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-783x178.jpg\" alt=\"\" width=\"643\" height=\"146\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-783x178.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-300x68.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-768x175.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-50x11.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-1536x349.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-60x14.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5-100x23.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/5.jpg 1918w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>Next click SQL from the row of options at the top of the page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-5192\" src=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-783x176.jpg\" alt=\"\" width=\"643\" height=\"145\" srcset=\"https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-783x176.jpg 783w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-300x67.jpg 300w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-768x173.jpg 768w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-50x11.jpg 50w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-1536x345.jpg 1536w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-60x13.jpg 60w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6-100x22.jpg 100w, https:\/\/chemicloud.com\/kb\/wp-content\/uploads\/2021\/04\/6.jpg 1918w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>A white box will appear. You are able to enter text into this box, but you won&#8217;t need to. Instead, click the &#8220;Choose File&#8221; button beneath the text box.<\/p>\n<p>When the file picker opens, choose the .postgresql file you wish to upload.<\/p>\n<p>Then click Execute to instruct phpPgAdmin to import the data in that file into the database.<\/p>\n<h2 id=\"additional-information\">Additional Information<\/h2>\n<p>The official documentation for PostgreSQL can be very handy sometimes.<\/p>\n<p>Find official PostgreSQL documentation <a href=\"https:\/\/www.postgresql.org\/docs\/\" target=\"_blank\" rel=\"noopener nofollow\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time you may need to export or import a PostgreSQL database. For example, you might be moving the database from one hosting provider to another, importing a database from a third party for repair or migrations, or you may be taking a backup of the database. This&#8230;<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"ht-kb-category":[224],"ht-kb-tag":[],"class_list":["post-5183","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-postgresql-databases"],"_links":{"self":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/comments?post=5183"}],"version-history":[{"count":9,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5183\/revisions"}],"predecessor-version":[{"id":7247,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5183\/revisions\/7247"}],"wp:attachment":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/media?parent=5183"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-category?post=5183"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-tag?post=5183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}