<?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: Siraphob Kan</title>
    <description>The latest articles on DEV Community by Siraphob Kan (@siraphobk).</description>
    <link>https://dev.to/siraphobk</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%2F512631%2F9b99391a-f1ec-4082-8fe6-163e59daf2f7.jpeg</url>
      <title>DEV Community: Siraphob Kan</title>
      <link>https://dev.to/siraphobk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siraphobk"/>
    <language>en</language>
    <item>
      <title>Make Neovim your ultimate code editor</title>
      <dc:creator>Siraphob Kan</dc:creator>
      <pubDate>Tue, 21 Dec 2021 15:27:47 +0000</pubDate>
      <link>https://dev.to/siraphobk/make-neovim-your-ultimate-code-editor-2j53</link>
      <guid>https://dev.to/siraphobk/make-neovim-your-ultimate-code-editor-2j53</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;; My dot-file can be downloaded from this &lt;a href="https://gist.github.com/siraphobk/abb53e7312e7f7fd099dd3c9cb276688" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prologue
&lt;/h2&gt;

&lt;p&gt;Vim was invented around 30 years ago (1991). It is a text editor that runs in a terminal. Programmers at the time use Vim to write their code.&lt;/p&gt;

&lt;p&gt;In mordern days, even with new text editor like VSCode, intellij, sublime text, etc; Vim still stand tall among them. This is because Vim has some advantages over those text editors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vim is extremely lightweight.&lt;br&gt;
Vim itself is very small and it's likely shipped with most Linux distros. The only dependency it needs is a terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;People who use Vim could edit their code faster.&lt;br&gt;
Vim enables full keyboard control. You can navigate, edit, delete, replace, etc. using only your keyboard. You'll seldomly need to touch your mouse when writing code ever again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim is highly customizable.&lt;br&gt;
You can configure Vim to work in your way. Keybindings are not intuitively enough? You can remap it! Want to add Git integration? You can do that too! Add code-completiong and linting for your programming language? I don't need to say that it's achievable too.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Neovim?
&lt;/h2&gt;

&lt;p&gt;You may wonder why I was talking about Vim when the title said Neovim. Well, here I am. In this article I'm going to explain why we'll use Neovim instead of Vim.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Neovim?
&lt;/h3&gt;

&lt;p&gt;Neovim is a forked version of Vim. It's basically Vim but driven by an open-source community. On the other hand, Vim is maintained by only one person which is its original author, Bram Moolenaar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why should you use Neovim instead of Vim?
&lt;/h3&gt;

&lt;p&gt;As I said, Neovim is maintained by the community. And because of that, the tends software evolves faster and more compatible with the modern system. And by that term, I am talking about integration with other third-parties and bug fixes.&lt;/p&gt;

&lt;p&gt;Moreover, Neovim's software architecture was redesigned to be more extensible and could process task in multi-thread fashion.&lt;/p&gt;

&lt;p&gt;With all that; the traits of its predecessor, blazingly fast software, and powerful plugins; is the reason I recommend you to use Neovim over regular Vim.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Neovim
&lt;/h3&gt;

&lt;p&gt;If you're on Debain-based system, you could run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;neovim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For other distributions/system, please refer to this &lt;a href="https://github.com/neovim/neovim/wiki/Installing-Neovim" rel="noopener noreferrer"&gt;wiki page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After the installation completed, you could start Neovim by just typing &lt;code&gt;neovim&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Neovim
&lt;/h2&gt;

&lt;p&gt;Before any processes of Vim begins, Vim will read its configuration file first.&lt;br&gt;
So does Neovim. Neovim's configuration file is located at &lt;code&gt;$HOME/.config/nvim/init.vim&lt;/code&gt;. If your system doesn't have this directory, you could create one.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;init.vim&lt;/code&gt; file allows you to specify how Neovim should behave. The look&lt;br&gt;
and feel, keybindings, and plugins are all configured through this file.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing plugins using Vim-Plug
&lt;/h3&gt;

&lt;p&gt;A way to install plugins in Neovim is through a plugin manager. There exists many plugin managers for you to choose. Here is some examples; Vim-Plug, Pathogen, Vundle, etc. In this article, I'm going to use Vim-Plug since it's very easy to use and setup.&lt;/p&gt;

&lt;p&gt;To install Vim-Plug for Neovim, please run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Neovim configuration file
&lt;/h3&gt;

&lt;p&gt;After the installation completed, open &lt;code&gt;init.vim&lt;/code&gt; and add the following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="k"&gt;call&lt;/span&gt; plug#begin&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'~/.config/nvim/plugged'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;" This is where you define your plugins.&lt;/span&gt;

&lt;span class="k"&gt;call&lt;/span&gt; plug#end&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet above make Vim-Plug scans for the defined plugins. Any defined plugins that don't exist in &lt;code&gt;nvim/plugged&lt;/code&gt; directory will be download and install. To trigger the Vim-Plug installation, reload Neovim and type &lt;code&gt;:PlugInstall&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Protips&lt;/strong&gt;: If you're editing &lt;code&gt;init.vim&lt;/code&gt;, save the file by running &lt;code&gt;:w&lt;/code&gt; and then &lt;code&gt;:source %&lt;/code&gt; to source the current file. After that you can call &lt;code&gt;PlugInstall&lt;/code&gt; without needing to reload the Neovim process again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Finding plugins
&lt;/h3&gt;

&lt;p&gt;You can find any plugins for Vim on &lt;a href="https://vimawesome.com/" rel="noopener noreferrer"&gt;Vimawesome&lt;/a&gt;. When you select a plugin on Vimawesome, it'll show installation instruction for&lt;br&gt;
any plugin manager.&lt;/p&gt;

&lt;p&gt;For example, please see &lt;a href="https://vimawesome.com/plugin/fugitive-vim" rel="noopener noreferrer"&gt;Fugitive&lt;/a&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%2Fiwq71vw2rols8xz1gy0k.png" 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%2Fiwq71vw2rols8xz1gy0k.png" alt="Fugitive Vim-Awesome"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Useful plugins I recommend.
&lt;/h3&gt;

&lt;p&gt;I am going to list all the plugins I use and explain what they do in my Neovim configuration. The list is shown below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;NERDTree - File browsing &lt;br&gt;
Plug 'scrooloose/nerdtree'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NERDTree Git Plugin - Git integration for NERDTree&lt;br&gt;
Plug 'Xuyuanp/nerdtree-git-plugin'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim Devicons - Extend Vim to use beatiful icons&lt;br&gt;
Plug 'ryanoasis/vim-devicons'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Syntastic - Syntax Checking&lt;br&gt;
Plug 'scrooloose/syntastic'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gitgutter - Show git diff in a file&lt;br&gt;
Plug 'airblade/vim-gitgutter'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NERDCommenter - Add comment keybindings support&lt;br&gt;
Plug 'scrooloose/nerdcommenter'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim Surround - Brackets wrapping &lt;br&gt;
Plug 'tpope/vim-surround'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CTRL-P - File search&lt;br&gt;
Plug 'ctrlpvim/ctrlp.vim'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim-Go - Go programming language support for Vim&lt;br&gt;
Plug 'fatih/vim-go'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;COC(Conquer Of Completion) - Syntax completion support&lt;br&gt;
Plug 'neoclide/coc.nvim', {'branch': 'release'}&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Colorschemes - Customize Vim colorscheme&lt;br&gt;
Plug 'flazz/vim-colorschemes'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim-Javascript - Javascript support &lt;br&gt;
Plug 'pangloss/vim-javascript'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim-Graphql - Graphql Sytax highlighting&lt;br&gt;
Plug 'jparise/vim-graphql'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Indentline - Show indentation guide in Vim&lt;br&gt;
Plug 'yggdroot/indentline'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FZF - FuzzyFinder support for Vim&lt;br&gt;
Plug 'junegunn/fzf', { 'do': { -&amp;gt; fzf#install() } }&lt;br&gt;
Plug 'junegunn/fzf.vim'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fugitive - Git integration&lt;br&gt;
Plug 'tpope/vim-fugitive'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vim-Airline - Vim status bar customization&lt;br&gt;
Plug 'vim-airline/vim-airline'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Undotree - Undo history &lt;br&gt;
Plug 'mbbill/undotree'&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  My dot-file
&lt;/h3&gt;

&lt;p&gt;For anyone who's looking for my dot-file. You can download it from this &lt;a href="https://gist.github.com/siraphobk/abb53e7312e7f7fd099dd3c9cb276688" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing thoughts
&lt;/h2&gt;

&lt;p&gt;In my opinion Neovim is not the thing that every developers should adopt. It is also not something that would instantly replace your current text editor. Honestly, I think the best text editor is not the fastest one or the one with the most plugins. The best text editor is the one that suits your workflow the most.&lt;/p&gt;

&lt;p&gt;Neovim is one of those text editor that fits my workflow. It is fast, lightweight, and highly customizable. It is a little bit harder to learn at first because of its steep learning curve. But I assure you that it is worth learning and it will pay off a great prize!&lt;/p&gt;

</description>
      <category>vim</category>
      <category>neovim</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to persist and backup PostgreSQL Docker container.</title>
      <dc:creator>Siraphob Kan</dc:creator>
      <pubDate>Thu, 25 Nov 2021 03:23:07 +0000</pubDate>
      <link>https://dev.to/siraphobk/how-to-persist-and-backup-postgresql-docker-container-b75</link>
      <guid>https://dev.to/siraphobk/how-to-persist-and-backup-postgresql-docker-container-b75</guid>
      <description>&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%2F8fnlryqovmuvebmee1ss.png" 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%2F8fnlryqovmuvebmee1ss.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Persisting database information with a mounted volume&lt;/li&gt;
&lt;li&gt;Dumping and restoring database information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever lose database information using a PostgreSQL docker container? We all know that we should never use a docker container to hold database data because when deleted, the data is gone with it, right? right???&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OMG YOU DID.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And so did I. There was one time when I started learning how to use Docker containers. Docker containers are very handy and they are operating system independent. The world is covered with sunshine and rainbows. Shit happens when it comes to a database container.&lt;/p&gt;

&lt;p&gt;At the time, I forgot to think about where the container holds its information. I was once use a postgresql container as a database instance for my application. Before deploying for production, I thought I should restart this once to refresh it. (Idk why but yea…) I typed &lt;code&gt;docker-compose down&lt;/code&gt; followed with &lt;code&gt;docker-compose up&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It was a good 1 minute of thinking that everything was ready for production and Imma leave and watch Netflix. And then, a panic comes in. I opened my application and found out that all my tables, all my configurations were gone into oblivion. The result was I had to reconfigure everything. Took me quite some time to get it working again. Good thing is at least it didn’t happen during production.&lt;/p&gt;

&lt;p&gt;In this article, I will show you how to persist data of a postgresql docker container and how to restore them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persisting PostgreSQL database information with a mounted volume
&lt;/h2&gt;

&lt;p&gt;We’ll first create our database using docker-compose. Please copy the following script into a docker-compose file then run &lt;code&gt;docker-compose up&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.1'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myuser&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mypassword&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mydb&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
       &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./data:/var/lib/postgresql/data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please notice on the volumes block. You can see that we mounted a volume &lt;code&gt;data/&lt;/code&gt; in the host machine to &lt;code&gt;/var/lib/postgresql/data&lt;/code&gt; directory of the postgresql container. This is a necessary step to do in order to persist data on the host machine. Because when the container is deleted, this directory will continue to exist.&lt;/p&gt;

&lt;p&gt;After you start the container, you’ll see a &lt;code&gt;data/&lt;/code&gt; directory showed up in your host machine. This directory is where all postgresql information is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ll
total 16
drwxr-xr-x  3 siraphob-wsl-ubuntu siraphob-wsl-ubuntu 4096 Nov 21 16:29 ./
drwxr-xr-x  4 siraphob-wsl-ubuntu siraphob-wsl-ubuntu 4096 Nov 21 16:26 ../
drwx------ 19                 999 root                4096 Nov 21 16:29 data/                &amp;lt;&lt;span class="nt"&gt;--&lt;/span&gt; HERE
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;  1 siraphob-wsl-ubuntu siraphob-wsl-ubuntu  232 Nov 21 16:28 docker-compose.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s test that our theory is correct. Shell into the database container by running the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &amp;lt;your-postgres-container-id&amp;gt; bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you’re inside the container, run the following command to connect to the postgresql console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;psql &lt;span class="nt"&gt;-d&lt;/span&gt; mydb &lt;span class="nt"&gt;-U&lt;/span&gt; myuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s create a table and insert some data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;serial&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'rick'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'morty'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you select rows from the table accounts, you’ll see the following result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;mydb&lt;/span&gt;&lt;span class="o"&gt;=#&lt;/span&gt; &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;rick&lt;/span&gt;
  &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;morty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, exit from the the container. And run docker-compose down to stop the container and remove it. Technically, everything should be gone. But as you can see that the data/ directory still persists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# stop and remove container&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose down
Stopping pg-persist-ex_db_1 ... &lt;span class="k"&gt;done
&lt;/span&gt;Removing pg-persist-ex_db_1 ... &lt;span class="k"&gt;done
&lt;/span&gt;Removing network pg-persist-ex_default
&lt;span class="c"&gt;# container is gone&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
&lt;span class="c"&gt;# data directory still persists&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;data  docker-compose.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, run docker-compose up again to start the database container. If you shell into the container and log in into the postgresql console. You can see that your table data isn’t lost.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;mydb&lt;/span&gt;&lt;span class="o"&gt;=#&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;
         &lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;relations&lt;/span&gt;
 &lt;span class="k"&gt;Schema&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="n"&gt;Name&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;Owner&lt;/span&gt;
&lt;span class="c1"&gt;--------+----------+-------+--------&lt;/span&gt;
 &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;myuser&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mydb&lt;/span&gt;&lt;span class="o"&gt;=#&lt;/span&gt; &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;
&lt;span class="c1"&gt;----+----------&lt;/span&gt;
  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;rick&lt;/span&gt;
  &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;morty&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s how you persist database information of a docker-container.&lt;/p&gt;

&lt;p&gt;Dumping and restoring PostgreSQL database information&lt;br&gt;
Another way to backup database information is to dump it out. Database dump is an export utility that helps you export database meta-data and data rows into a file. The dump file could later be imported into a new database.&lt;/p&gt;

&lt;p&gt;Dumping allows you to export only meta-data (schemas, tables, relationships) or both meta-data and data rows. In this case, I’m going to show you how to export all information from a database.&lt;/p&gt;

&lt;p&gt;To backup your postgres database from a docker container, please run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;your-postgres-container-id&amp;gt; pg_dumpall &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dump_&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%d-%m-%Y&lt;span class="s2"&gt;"_"&lt;/span&gt;%H_%M_%S&lt;span class="sb"&gt;`&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you open the dump file, scroll down and you’ll see that it has our database information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="o"&gt;#&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;opened&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;vim&lt;/span&gt;
&lt;span class="mi"&gt;179&lt;/span&gt; &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="mi"&gt;180&lt;/span&gt;     &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="mi"&gt;181&lt;/span&gt;     &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="nb"&gt;character&lt;/span&gt; &lt;span class="nb"&gt;varying&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="mi"&gt;182&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;183&lt;/span&gt;
&lt;span class="mi"&gt;184&lt;/span&gt;
&lt;span class="mi"&gt;185&lt;/span&gt; &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;OWNER&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;myuser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="mi"&gt;216&lt;/span&gt; &lt;span class="c1"&gt;--&lt;/span&gt;
&lt;span class="mi"&gt;217&lt;/span&gt; &lt;span class="c1"&gt;-- Data for Name: accounts; Type: TABLE DATA; Schema: public; Owner: myuser&lt;/span&gt;
&lt;span class="mi"&gt;218&lt;/span&gt; &lt;span class="c1"&gt;--&lt;/span&gt;
&lt;span class="mi"&gt;219&lt;/span&gt;
&lt;span class="mi"&gt;220&lt;/span&gt; &lt;span class="k"&gt;COPY&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;221&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="n"&gt;rick&lt;/span&gt;
&lt;span class="mi"&gt;222&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;   &lt;span class="n"&gt;morty&lt;/span&gt;
&lt;span class="mi"&gt;223&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to restore database information from a dump file, please run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;your_dump.sql | docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &amp;lt;your-postgres-container-id&amp;gt; psql &lt;span class="nt"&gt;-U&lt;/span&gt; myuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could try this on your own on a fresh postgres container to see the effect.&lt;/p&gt;

&lt;p&gt;And that’s it for today. Now you’ll never lose your database information again. Happy coding!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>dockercompose</category>
      <category>tutorial</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
