{"id":1178,"date":"2021-04-07T15:44:25","date_gmt":"2021-04-07T12:44:25","guid":{"rendered":"https:\/\/www.scalahosting.com\/kb\/?p=1178"},"modified":"2025-11-27T11:46:29","modified_gmt":"2025-11-27T09:46:29","slug":"how-to-import-and-export-postgresql-databases","status":"publish","type":"post","link":"https:\/\/www.scalahosting.com\/kb\/how-to-import-and-export-postgresql-databases\/","title":{"rendered":"Import Export PostgreSQL Database Guide"},"content":{"rendered":"<p><b>Importing and exporting data from PostgreSQL databases can be critical when you&#8217;re trying to move your website to a new server or when you&#8217;re trying to back it up. <\/b>Doing it is not that difficult.<br \/>\n<span style=\"font-weight: 400;\">So, let\u2019s get started.<\/span><\/p>\n<h2><b>Exporting PostgreSQL Databases<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Exporting your Postgres database helps you download your data for safe-keeping or transfer it to another server. You could export the database in two ways.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And, here\u2019s how.<\/span><\/p>\n<h3><b>Method 1: via The Command Line<\/b><\/h3>\n<p><b>You can export a Postgres database via the command line<\/b><span style=\"font-weight: 400;\"> by running the <\/span><i><span style=\"font-weight: 400;\">pg_dump<\/span><\/i><span style=\"font-weight: 400;\"> command. Follow these steps to get started.<\/span><\/p>\n<p><a href=\"https:\/\/www.enterprisedb.com\/downloads\/postgres-postgresql-downloads\" rel=\"nofollow\"><span style=\"font-weight: 400;\">Install Postgres<\/span><\/a><span style=\"font-weight: 400;\"> on your local computer to access your Postgres databases remotely. Of course, ensure to install only the latest stable version.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Open the command line to run tasks on your Postgres database. But if you\u2019ve physical access to the server,<\/span><b> open a DOS or terminal window<\/b><span style=\"font-weight: 400;\"> to access the command line.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Run this command to export any database.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">pg_dump -U username dbname &gt; dbbackup.pgsql<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">You\u2019ll need to replace the <\/span><i><span style=\"font-weight: 400;\">username<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">dbname,<\/span><\/i><span style=\"font-weight: 400;\"> and <\/span><i><span style=\"font-weight: 400;\">dbbackup.pgsql<\/span><\/i><span style=\"font-weight: 400;\"> placeholders with the account username, database name, and the desired output file\u2019s name, respectively.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Type your password when prompted to run the database export.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The above command means you want <\/span><span style=\"font-weight: 400;\">to export data from <\/span><b>a database called <\/b><b><i>dbname<\/i><\/b> <b>on a local PostgreSQL server into a file called <\/b><b><i>dbbackup.pgsql.<\/i><\/b><\/p>\n<p>You can use FTP to download the file.<\/p>\n<h3><b>Method 2: via phpPgAdmin<\/b><\/h3>\n<p><b>Managing Postgres databases via the phpPgAdmin is much easier<\/b> since you won\u2019t need to use the command-line.<\/p>\n<p>phpPgAdmin is Postgres\u2019 equivalent for MySQL\u2019s phpMyAdmin, and both allow users to manage their databases from a web interface.<\/p>\n<p>First, make sure phpPgAdmin is installed on your server. If it&#8217;s not, you might need to install it yourself, which requires root access.<\/p>\n<p>With phpPgAdmin installed, you can log into its web interface and follow the steps below to export and download the data.<\/p>\n<ul>\n<li aria-level=\"1\">Locate the database you intend to export.<\/li>\n<li aria-level=\"1\">Click <b>Export<\/b> on the top menu bar.<\/li>\n<li aria-level=\"1\">For the export options, select <b>SQL<\/b> as the file format and click on <b>Download.<\/b><\/li>\n<li aria-level=\"1\">Click on <b>Export<\/b> to run the task.<\/li>\n<li aria-level=\"1\">Select the location to save the file and click on <b>Save<\/b>.<\/li>\n<\/ul>\n<h2><b>Importing PostgreSQL Databases<\/b><\/h2>\n<p>The Postgres import feature allows users to restore backed-up files, add new files to a Postgres database or transfer a database to a new server.<\/p>\n<p>You can run the import tasks in two ways. Let\u2019s explore them.<\/p>\n<h3><b>Method 1: via The Command Line<\/b><\/h3>\n<p>You can import files to\u00a0 Postgres via the command line using the psql program<b>. Follow these steps to get started.<\/b><\/p>\n<ul>\n<li aria-level=\"1\">Here, we\u2019re assuming you saved the file as <b><i>dbbackup.pgsql<\/i><\/b><b>.<\/b><\/li>\n<li aria-level=\"1\">To import this file, transfer it to your server using FTP, SCM or any file transfer protocol.<\/li>\n<li aria-level=\"1\">Open the psql command line and run this command.<\/li>\n<\/ul>\n<p><i>psql -U username dbname &lt; dbbackup.pgsql<\/i><\/p>\n<p>Replace the <i>username<\/i> and <i>dbname<\/i> placeholders with your username and the database name you want to import the file into, respectively.<\/p>\n<h3><b>Method 2: via phpPgAdmin<\/b><\/h3>\n<p>Importing files via phpPgAdmin makes it much more manageable. Follow these steps to get started.<\/p>\n<ul>\n<li aria-level=\"1\">Log in to your phpPgAdmin web console.<\/li>\n<li aria-level=\"1\">Locate the database you intend to import files into and click on it.<\/li>\n<li aria-level=\"1\">Click on <b>SQL<\/b> on the top menu bar.<\/li>\n<li aria-level=\"1\">Click <b>Choose File<\/b> and select the file you want to import.<\/li>\n<li aria-level=\"1\">Click <b>Execute<\/b> to run the task.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Importing and exporting data from PostgreSQL databases can be critical when you&#8217;re trying to move your website to a new server or when you&#8217;re trying to back it up. Doing it is not that difficult. So, let\u2019s get started. Exporting PostgreSQL Databases Exporting your Postgres database helps you download your data for safe-keeping or transfer [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"none","_seopress_titles_title":"Import Export PostgreSQL Database Guide | ScalaHosting KB","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_analysis_target_kw":"","footnotes":""},"categories":[34],"tags":[],"class_list":["post-1178","post","type-post","status-publish","format-standard","hentry","category-web-hosting"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/comments?post=1178"}],"version-history":[{"count":12,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1178\/revisions"}],"predecessor-version":[{"id":5844,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1178\/revisions\/5844"}],"wp:attachment":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/media?parent=1178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/categories?post=1178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/tags?post=1178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}