<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shafayat Jamil</title>
    <description>The latest articles on DEV Community by Shafayat Jamil (@jamii).</description>
    <link>https://dev.to/jamii</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1265153%2F9c1a4163-2b71-4c34-b673-8f50f891b401.jpeg</url>
      <title>DEV Community: Shafayat Jamil</title>
      <link>https://dev.to/jamii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamii"/>
    <language>en</language>
    <item>
      <title>Backup and Restore Postgres Database from local to remote.</title>
      <dc:creator>Shafayat Jamil</dc:creator>
      <pubDate>Mon, 29 Jan 2024 12:50:37 +0000</pubDate>
      <link>https://dev.to/jamii/backup-and-restoring-postgres-database-from-local-to-remote-2e90</link>
      <guid>https://dev.to/jamii/backup-and-restoring-postgres-database-from-local-to-remote-2e90</guid>
      <description>&lt;p&gt;Our application runs on the production server smoothly. But we don't have any test/QA environment where some completed modules can be tested. So my team-lead assigned me a task to deploy the application in the QA environment. The first&lt;br&gt;
challenge i faced was not having a backup database. As these completed modules can't be tested with empty data.&lt;br&gt;
After thinking for a few minutes, I made a plan to do the task.&lt;/p&gt;

&lt;p&gt;My plan was : &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dumping the local database&lt;/strong&gt; -&amp;gt; &lt;strong&gt;copy the dump file to the remote server&lt;/strong&gt; -&amp;gt; &lt;strong&gt;create a database in remote server&lt;/strong&gt; -&amp;gt; &lt;strong&gt;restore the dump file in newly created database in server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now I will go through every step elaborately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dumping database using pg_dump
&lt;/h2&gt;

&lt;p&gt;I used &lt;code&gt;pg_dump&lt;/code&gt; to create backup files from my local database as &lt;code&gt;pg_dump&lt;/code&gt; is flexible and powerful utility for backing up PostgreSQL databases.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pg_dump -U &amp;lt;username&amp;gt; -h localhost -d &amp;lt;database_name&amp;gt; -F c -f &amp;lt;backup_file_name.dump


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Break down of the command are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-U Specifies the PostgreSQL username to connect to the database. &lt;/li&gt;
&lt;li&gt;-h Specifies the host where the PostgreSQL server is running&lt;/li&gt;
&lt;li&gt;-d Specifies the name of the database to dump&lt;/li&gt;
&lt;li&gt;-F c Specifies the format of the output file. c means custom format.&lt;/li&gt;
&lt;li&gt;-f Specifies the name of the file to which the database dump will be written.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After executing the command, a sql file will be created.&lt;/p&gt;

&lt;h2&gt;
  
  
  Move the dump file to the remote server
&lt;/h2&gt;

&lt;p&gt;Our next work is to copy the dump file to the remote server. To do that I used &lt;code&gt;scp&lt;/code&gt; command. &lt;code&gt;scp&lt;/code&gt; command is used for securely copying files between a local host and a remote host.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

scp file_path/file_name remote_username@remote_ip_address


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;file_path&lt;/code&gt; and &lt;code&gt;file_name&lt;/code&gt; specify the file to copy. &lt;code&gt;remote_username&lt;/code&gt; and &lt;code&gt;ip_address&lt;/code&gt; is the remote destination to specify where the file should be copied. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Database in remote.
&lt;/h2&gt;

&lt;p&gt;My next target was to create a database in remote machine where I would restore the data.To create a database we can either execute the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

createdb -U username -h localhost -p port_number -e db_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or we can use psql console. &lt;code&gt;psql&lt;/code&gt; is a command-line interface (CLI) utility for interacting with PostgreSQL databases.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo su postgres


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It will ask for password. After completing authentication we have to write &lt;code&gt;psql&lt;/code&gt; and press enter. And then it will take us to the psql console.&lt;br&gt;
In psql console we can create database by following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

create database db_name;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It will create a new database. To view the list of databases that exists in our remote machine, we can type &lt;code&gt;\l&lt;/code&gt; and it will show the list of databases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa81m8zv5rtnp0k4x1817.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa81m8zv5rtnp0k4x1817.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Restore the data
&lt;/h2&gt;

&lt;p&gt;To restore data, I used &lt;code&gt;pg_restore&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pg_restore -d database_name file_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;-d database_name&lt;/code&gt; Specifies the name of the target database where we want to restore the data. &lt;code&gt;file_name&lt;/code&gt; is our backup file which we copied. Whenever we execute this command, data will be restored in our newly created database. To view data is present in the database, we can type:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

psql -U username -h localhost -p port_number -d db_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's break down the command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-U username&lt;/code&gt; Specifies the PostgreSQL username to connect to the database&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-h localhost&lt;/code&gt; Specifies the host where the PostgreSQL server is running.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p port_number&lt;/code&gt; Specifies the port number on which the PostgreSQL server is listening&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-d db_name&lt;/code&gt; Specifies the name of the database to connect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command will connect to our database. If we type &lt;code&gt;\dt;&lt;/code&gt; it will show all the table inside the database. To view more details, we can type &lt;code&gt;\dt+;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we want to connect to the database through our application, we have to edit &lt;code&gt;pg_hba.conf&lt;/code&gt; file of postgres. It is a configuration file used by PostgreSQL to control client authentication to the server. In this file, administrators can define rules that specify which hosts are allowed to connect, which users they can connect as, and the authentication methods they should use.&lt;br&gt;
We have to entry our port, host and authentication type under &lt;code&gt;replication privilege&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzaid5f6gudmj60kxqknt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzaid5f6gudmj60kxqknt.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To edit &lt;code&gt;pg_hba.conf&lt;/code&gt; file we have to enter the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo nano file_path/pg_hba.conf;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To find out where hba.conf file located in, we can type &lt;code&gt;SHOW hba_file;&lt;/code&gt;. It will show the path of conf file.&lt;/p&gt;

&lt;p&gt;That's it. Hope You will get benefit from it.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>psql</category>
      <category>bash</category>
      <category>database</category>
    </item>
  </channel>
</rss>
