<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>databases Archives - Tech Chronicles</title>
	<atom:link href="http://kostacipo.stream/tag/databases/feed/" rel="self" type="application/rss+xml" />
	<link>https://kostacipo.stream/tag/databases/</link>
	<description>Ramblings of a Tech Dude</description>
	<lastBuildDate>Fri, 31 Jan 2020 10:55:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>

<image>
	<url>https://kostacipo.stream/wp-content/uploads/2019/12/cropped-profile-32x32.jpg</url>
	<title>databases Archives - Tech Chronicles</title>
	<link>https://kostacipo.stream/tag/databases/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Automating PostgreSQL Daily Tasks Using Jenkins</title>
		<link>http://kostacipo.stream/automating-postgresql-daily-tasks-using-jenkins/</link>
					<comments>http://kostacipo.stream/automating-postgresql-daily-tasks-using-jenkins/#respond</comments>
		
		<dc:creator><![CDATA[Majordomo]]></dc:creator>
		<pubDate>Fri, 31 Jan 2020 10:55:29 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[jenkins]]></category>
		<category><![CDATA[postgresql]]></category>
		<guid isPermaLink="false">http://www.kostacipo.stream/?p=1729</guid>

					<description><![CDATA[<p>&#160; About Jenkins Jenkins&#160;is an open source software for automation. It is developed in java and is one of the most popular tools for Continuous Integration (CI) and Continuous Delivery (CD). In 2010, after the acquisition of Sun Microsystems by Oracle, &#8220;Hudson&#8221; software was in a dispute with its open source community. This dispute became [&#8230;]</p>
<p>The post <a href="http://kostacipo.stream/automating-postgresql-daily-tasks-using-jenkins/">Automating PostgreSQL Daily Tasks Using Jenkins</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<h2>About Jenkins</h2>
<p><a href="https://en.wikipedia.org/wiki/Jenkins_(software)" target="_blank" rel="nofollow noopener noreferrer">Jenkins</a>&nbsp;is an open source software for automation. It is developed in java and is one of the most popular tools for Continuous Integration (CI) and Continuous Delivery (CD).</p>
<p>In 2010, after the acquisition of Sun Microsystems by Oracle, &#8220;<a href="https://en.wikipedia.org/wiki/Hudson_(software)" target="_blank" rel="nofollow noopener noreferrer">Hudson</a>&#8221; software was in a dispute with its open source community. This dispute became the basis for the launch of the Jenkins project.</p>
<p>Nowadays, &#8220;Hudson” (Eclipse public license) and “Jenkins” (MIT license) are two active and independent projects with a very similar purpose.</p>
<p>Jenkins has thousands of plugins you can use in order to speed up the development phase through automation for the entire development life-cycle; build, document, test, package, stage and deployment.</p>
<h2>What Does Jenkins Do?</h2>
<p>Although the main use of Jenkins could be Continuous Integration (CI) and Continuous Delivery (CD), this open source has a set of functionalities and it can be used without any commitment or dependence from CI or CD, thus Jenkins presents some interesting functionalities to explore:</p>
<ul>
<li>Scheduling period jobs (instead of using the traditional&nbsp;<em>crontab</em>)</li>
<li>Monitoring jobs, its logs and activities by a clean view (as they have an option for grouping)</li>
<li>Maintenance of jobs could be done easily; assuming Jenkins has a set of options for it</li>
<li>Setup and scheduling software installation (by using Puppet) in the same host or in another one.</li>
<li>Publishing reports and sending email notifications</li>
</ul>
<h2>Running PostgreSQL Tasks in Jenkins</h2>
<p>There are three common tasks a&nbsp;PostgreSQL&nbsp;developer or database administrator has to do on a daily basis:</p>
<ol>
<li>Scheduling and execution of PostgreSQL scripts</li>
<li>Executing a PostgreSQL process composed of three or more scripts</li>
<li>Continuous Integration (CI) for PL/pgSQL developments</li>
</ol>
<p>For the execution of these examples, it is assumed that Jenkins and PostgreSQL (at least the version 9.5) servers are installed and working properly.</p>
<h2>Scheduling and Execution of a PostgreSQL Script</h2>
<p>In most cases the implementation of daily (or periodically) PostgreSQL scripts for the execution of a usual task such as&#8230;</p>
<ul>
<li>Generation of backups</li>
<li>Test the restore of a backup</li>
<li>Execution of a query for reporting purposes</li>
<li>Clean up and archiving log files</li>
<li>Calling a PL/pgSQL procedure to purge tables</li>
</ul>
<p>t’s defined on&nbsp;<em>crontab</em>:</p>
<div>
<div id="highlighter_924287" class="syntaxhighlighter  bash">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash plain">0 5,17 * * * </code><code class="bash plain">/filesystem/scripts/archive_logs</code><code class="bash plain">.sh</code></div>
<div class="line number2 index1 alt1"><code class="bash plain">0 2 * * * </code><code class="bash plain">/db/scripts/db_backup</code><code class="bash plain">.sh</code></div>
<div class="line number3 index2 alt2"><code class="bash plain">0 6 * * * </code><code class="bash plain">/db/data/scripts/backup_client_tables</code><code class="bash plain">.sh</code></div>
<div class="line number4 index3 alt1"><code class="bash plain">0 4 * * * </code><code class="bash plain">/db/scripts/Test_db_restore</code><code class="bash plain">.sh</code></div>
<div class="line number5 index4 alt2"><code class="bash plain">*</code><code class="bash plain">/10</code> <code class="bash plain">* * * * </code><code class="bash plain">/db/scripts/monitor</code><code class="bash plain">.sh</code></div>
<div class="line number6 index5 alt1"><code class="bash plain">0 4 * * * </code><code class="bash plain">/db/data/scripts/queries</code><code class="bash plain">.sh</code></div>
<div class="line number7 index6 alt2"><code class="bash plain">0 4 * * * </code><code class="bash plain">/db/scripts/data_extraction</code><code class="bash plain">.sh</code></div>
<div class="line number8 index7 alt1"><code class="bash plain">0 5 * * * </code><code class="bash plain">/db/scripts/data_import</code><code class="bash plain">.sh</code></div>
<div class="line number9 index8 alt2"><code class="bash plain">0 *</code><code class="bash plain">/4</code> <code class="bash plain">* * * </code><code class="bash plain">/db/data/scripts/report</code><code class="bash plain">.sh</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>As the&nbsp;<em>crontab</em>&nbsp;isn’t the best user friendly tool to manage this kind of scheduling, it can be done on Jenkins with the following advantages&#8230;</p>
<ul>
<li>Very friendly interface to monitor their progress and current status</li>
<li>The logs are immediately availables and no need any special grant to access them</li>
<li>The job could be executed manually on Jenkins instead to have a scheduling</li>
<li>For some kind of jobs, no need to define users and passwords in plain text files as Jenkins do it in a secure way</li>
<li>The jobs could be defined as an API execution</li>
</ul>
<p>So, it could be a good solution to migrate the jobs related to PostgreSQL tasks to Jenkins instead of crontab.</p>
<p>On the other hand, most database administrators and developers have strong skills in scripting languages and it would be easy for them to develop small interfaces to deal with these scripts to implement the automated processes with the goal of improving their tasks. But remember, Jenkins most likely already has a set of functions to do it and these functionalities can make life easy for developers who choose to use them.</p>
<div class="pblock size_4">&nbsp;</div>
<p>Thus to define the execution of script it’s necessary to create a new job, selecting the “New Item” option.</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image16.jpg" alt="Figure 1 – &quot;New Item&quot; in order to define a job to execute a PostgreSQL script"></p>
<div class="description">Figure 1 – &#8220;New Item&#8221; in order to define a job to execute a PostgreSQL script</div>
</div>
</div>
<p>Then, after naming it, choose the type “FreeStyle projects” and click OK.</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image2.jpg" alt="Figure 2 – Selection of the job (item) type"></p>
<div class="description">Figure 2 – Selection of the job (item) type</div>
</div>
</div>
<p>To finish the creation of this new job, in the section “Build” must be selected the option “Execute script” and in the command line box the path and parameterization of the script that will be executed:</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image7.png" alt="Figure 3 – Specification of the command to execute"></p>
<div class="description">Figure 3 – Specification of the command to execute</div>
</div>
</div>
<p>For this kind of job, it’s advisable to verify script permissions, because at least execution for the group the file belongs and for everyone must be set.</p>
<p>In this example, the script&nbsp;<em>query.sh</em>&nbsp;has read and execute permissions for everyone, read and execute permissions for the group and read write and execute for the user:</p>
<div>
<div id="highlighter_405244" class="syntaxhighlighter  bash">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash plain">slonik@severalnines:~</code><code class="bash plain">/db/scripts</code><code class="bash plain">$ </code><code class="bash functions">ls</code> <code class="bash plain">-l query.sh</code></div>
<div class="line number2 index1 alt1"><code class="bash plain">-rwxr-xr-x 1 slonik slonik 365 May 11 20:01 query.sh</code></div>
<div class="line number3 index2 alt2"><code class="bash plain">slonik@severalnines:~</code><code class="bash plain">/db/scripts</code><code class="bash plain">$</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This script has a very simple set of statements, basically only calls to the utility psql in order to execute queries:</p>
<div>
<div id="highlighter_190569" class="syntaxhighlighter  bash">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash preprocessor bold">#!/bin/bash</code></div>
<div class="line number2 index1 alt1">&nbsp;</div>
<div class="line number3 index2 alt2"><code class="bash plain">/usr/lib/postgresql/10/bin/psql</code> <code class="bash plain">-U report -d db_deploy -c </code><code class="bash string">"select * from appl"</code> <code class="bash plain">&gt; </code><code class="bash plain">/home/slonik/db/scripts/appl</code><code class="bash plain">.dat</code></div>
<div class="line number4 index3 alt1"><code class="bash spaces">&nbsp;</code>&nbsp;</div>
<div class="line number5 index4 alt2"><code class="bash plain">/usr/lib/postgresql/10/bin/psql</code> <code class="bash plain">-U report -d db_deploy -c </code><code class="bash string">"select * from appl_users"</code> <code class="bash plain">&gt; </code><code class="bash plain">/home/slonik/db/scripts/appl_user</code><code class="bash plain">.dat</code></div>
<div class="line number6 index5 alt1"><code class="bash spaces">&nbsp;</code>&nbsp;</div>
<div class="line number7 index6 alt2"><code class="bash plain">/usr/lib/postgresql/10/bin/psql</code> <code class="bash plain">-U report -d db_deploy -c </code><code class="bash string">"select * from appl_rights"</code> <code class="bash plain">&gt; </code><code class="bash plain">/home/slonik/db/scripts/appl_rights</code><code class="bash plain">.dat</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>Executing a PostgreSQL Process Composed of Three or More Scripts</h2>
<p>In this example, I’ll describe what you need to execute three different scripts in order to hide sensitive data and for that, we will follow the below steps&#8230;</p>
<ul>
<li>Import data from files</li>
<li>Prepare data to be masked</li>
<li>Backup of database with data masked</li>
</ul>
<p>So, to define this new job it’s necessary to select the option “New Item” in the Jenkins main page and then, after to assign a name, the “Pipeline” option must be chosen:</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image9.jpg" alt="Figure 5 - Pipeline item in Jenkins"></p>
<div class="description">Figure 5 &#8211; Pipeline item in Jenkins</div>
</div>
</div>
<p>Once the job is saved in the “Pipeline” section, on the tab “Advanced project options”, the “Definition” field must be set to “Pipeline script”, as shown below:</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image15.jpg" alt="Figure 6 - Groovy script in pipeline section"></p>
<div class="description">Figure 6 &#8211; Groovy script in pipeline section</div>
</div>
</div>
<p>As I mentioned at the beginning of the chapter, the used Groovy script it’s composed by three stages, it means three distinct parts (stages), as presented in the following script:</p>
<div>
<div id="highlighter_897752" class="syntaxhighlighter  java">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
<div class="line number17 index16 alt2">17</div>
<div class="line number18 index17 alt1">18</div>
<div class="line number19 index18 alt2">19</div>
<div class="line number20 index19 alt1">20</div>
<div class="line number21 index20 alt2">21</div>
<div class="line number22 index21 alt1">22</div>
<div class="line number23 index22 alt2">23</div>
<div class="line number24 index23 alt1">24</div>
<div class="line number25 index24 alt2">25</div>
<div class="line number26 index25 alt1">26</div>
<div class="line number27 index26 alt2">27</div>
<div class="line number28 index27 alt1">28</div>
<div class="line number29 index28 alt2">29</div>
<div class="line number30 index29 alt1">30</div>
<div class="line number31 index30 alt2">31</div>
<div class="line number32 index31 alt1">32</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="java plain">def dir_live1=</code><code class="java string">'/data/ftp/server1'</code></div>
<div class="line number2 index1 alt1"><code class="java plain">def dir_live2=</code><code class="java string">'/data/ftp/server2'</code></div>
<div class="line number3 index2 alt2">&nbsp;</div>
<div class="line number4 index3 alt1"><code class="java plain">pipeline{</code></div>
<div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">agent any</code></div>
<div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">stages</code></div>
<div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">{</code></div>
<div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">stage(</code><code class="java string">'Import data from files'</code><code class="java plain">){</code></div>
<div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">steps{</code></div>
<div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">echo </code><code class="java string">"Import data from ${dir_live1} ..."</code></div>
<div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sh script :</code><code class="java string">"/home/slonik/db/scripts/import_data_from_files.sh ${dir_live1}"</code></div>
<div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">echo </code><code class="java string">"Import data from $dir_live2 ..."</code></div>
<div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sh script :</code><code class="java string">"/home/slonik/db/scripts/import_data_from_files.sh ${dir_live2}"</code></div>
<div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">stage(</code><code class="java string">'Prepare data to be masked'</code><code class="java plain">){</code></div>
<div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">steps{</code></div>
<div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">echo </code><code class="java string">"Purge data ..."</code></div>
<div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sh script :</code><code class="java string">"/home/slonik/db/scripts/purge_data.sh"</code></div>
<div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">echo </code><code class="java string">"Mask data ..."</code></div>
<div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sh script :</code><code class="java string">"/home/slonik/db/scripts/mask_data.sh"</code></div>
<div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">stage(</code><code class="java string">'Backup of database with data masked'</code><code class="java plain">){</code></div>
<div class="line number25 index24 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">steps{</code></div>
<div class="line number26 index25 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">echo </code><code class="java string">"Backup database after masking ..."</code></div>
<div class="line number27 index26 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sh script :</code><code class="java string">"/home/slonik/db/scripts/backup_db.sh"</code></div>
<div class="line number28 index27 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number29 index28 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">} </code></div>
<div class="line number30 index29 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div>
<div class="line number31 index30 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div>
<div class="line number32 index31 alt1"><code class="java plain">}</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><a href="http://groovy-lang.org/" target="_blank" rel="nofollow noopener noreferrer">Groovy</a>&nbsp;is a Java-syntax-compatible object oriented programming language for the Java platform. It’s both a static and dynamic language with features similar to those of Python, Ruby, Perl and Smalltalk.</p>
<p>It’s easy to understand since this kind of script is based in a few statements…</p>
<h3>Stage</h3>
<p>Means the 3 processes that will be executed: “Import data from files”, “Prepare data to be masked”</p>
<p>and “Backup of database with data masked”.</p>
<h3>Step</h3>
<p>A “step” (often called a “build step”) is a single task that is part of a sequence. Each stage could be composed of several steps. In this example, the first stage has two steps.</p>
<div>
<div id="highlighter_759204" class="syntaxhighlighter  bash">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash plain">sh script :"</code><code class="bash plain">/home/slonik/db/scripts/import_data_from_files</code><code class="bash plain">.sh </code><code class="bash string">'/data/ftp/server1'</code></div>
<div class="line number2 index1 alt1">&nbsp;</div>
<div class="line number3 index2 alt2"><code class="bash plain">sh script :"</code><code class="bash plain">/home/slonik/db/scripts/import_data_from_files</code><code class="bash plain">.sh </code><code class="bash string">'/data/ftp/server2'</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="pblock size_4">&nbsp;</div>
<p>The data are being imported from two distinct sources.</p>
<p>In the previous example, it’s important to note that there are two variables defined at the beginning and with a global scope:</p>
<div>
<div id="highlighter_236971" class="syntaxhighlighter  bash">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash plain">dir_live1</code></div>
<div class="line number2 index1 alt1"><code class="bash plain">dir_live2</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The scripts used in these three steps are calling the&nbsp;<em>psql</em>,&nbsp;<em>pg_restore</em>&nbsp;and&nbsp;<em>pg_dump</em>&nbsp;utilities.</p>
<p>Once the job is defined, it’s time to execute it and for that, it’s only necessary to click the option “Build Now”:</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image10.jpg" alt="Figure 7 – Execution job"></p>
<div class="description">Figure 7 – Execution job</div>
</div>
</div>
<p>After the build starts it’s possible to verify its progress.</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image4.jpg" alt="Figure 8 – Starting “Build”"></p>
<div class="description">Figure 8 – Starting “Build”</div>
</div>
</div>
<p>The Pipeline Stage View plugin includes an extended visualization of Pipeline build history on the index page of a flow project under Stage View. This view is built as soon as the tasks are completed and each task is represented by column from the left to the right and it’s possible to view and compare the elapsed time for the serval executions (known as a Build in Jenkins).</p>
<p>Once the execution (also called a Build) finishes, it’s possible to get additional details, clicking on the finished thread (red box).</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image13.jpg" alt="Figure 9 – Starting “Build”"></p>
<div class="description">Figure 9 – Starting “Build”</div>
</div>
</div>
<p>and then in “Console Output” option.</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image14.jpg" alt="Figure 10 – Console output"></p>
<div class="description">Figure 10 – Console output</div>
</div>
</div>
<p>The previous views are extreme utility since they allow to have a perception of the runtime required of each stage.</p>
<p>Pipelines, also known as workflow, it’s a plugin that allows the definition of the application lifecycle and it’s a functionality used in Jenkins for Continuous delivery (CD).vThis plugin was built with requirements for a flexible, extensible and script-based CD workflow capability in mind.</p>
<p>This example is to hide sensitive data but for sure there are many other examples on a daily basis of PostgreSQL database administrator that can be executed on a pipeline job.</p>
<p>Pipeline has been available on Jenkins since version 2.0 and it’s an incredible solution!</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image12.jpg"></div>
</div>
<h2>Continuous Integration (CI) for PL/pgSQL Developments</h2>
<p>The continuous integration for the database development is not as easy as in other programming languages due to the data that can be lost, so it isn’t easy to keep the database in source control and deploy it on a dedicated server particularly once there are scripts that contain DDL (Data Definition Language) and DML (Data Manipulation Language) statements. This is because these kinds of statements modify the current state of the database and unlike other programming languages there is no source code to compile.</p>
<p>On the other hand, there are a set of database statements for which it’s possible the continuous integration as for other programming languages.</p>
<p>This example it’s based only in the development of procedures and it will illustrate the triggering of a set of tests (written in Python) by Jenkins once PostgreSQL scripts, on which are stored the code of the following functions, are committed in a code repository.</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image8.jpg" alt="Figure 11 – PLpg/SQL functions"></p>
<div class="description">Figure 11 – PLpg/SQL functions</div>
</div>
</div>
<p>These functions are simple and its content only have a few logic or a query in&nbsp;<em>PLpg/SQL</em>&nbsp;or&nbsp;<em>plperlu</em>&nbsp;language as the function&nbsp;<em>f_IsValidEmail</em>:</p>
<div>
<div id="highlighter_259774" class="syntaxhighlighter  sql">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="sql keyword">CREATE</code> <code class="sql color1">OR</code> <code class="sql color2">REPLACE</code> <code class="sql keyword">FUNCTION</code> <code class="sql plain">f_IsValidEmail(email text) </code><code class="sql keyword">RETURNS</code> <code class="sql plain">bool</code></div>
<div class="line number2 index1 alt1"><code class="sql plain">LANGUAGE plperlu</code></div>
<div class="line number3 index2 alt2"><code class="sql keyword">AS</code> <code class="sql plain">$$</code></div>
<div class="line number4 index3 alt1"><code class="sql plain">use Email::Address;</code></div>
<div class="line number5 index4 alt2"><code class="sql plain">my @addresses = Email::Address-&gt;parse($_[0]);</code></div>
<div class="line number6 index5 alt1"><code class="sql keyword">return</code> <code class="sql plain">scalar(@addresses) &gt; 0 ? 1 : 0;</code></div>
<div class="line number7 index6 alt2"><code class="sql plain">$$;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>All the functions presented here do not depend on each other, and then there is no precedence either in its development or in its deployment. Also, as it will be verified ahead, there is no dependence on their validations.</p>
<p>So, in order to execute a set of validation scripts once a commit it’s performed in a code repository it’s necessary the creation of a build job (new item) in Jenkins:</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image6.png" alt="Figure 12 – &quot;Freestyle” project for Continuous Integration"></p>
<div class="description">Figure 12 – &#8220;Freestyle” project for Continuous Integration</div>
</div>
</div>
<p>This new build job should be created as “Freestyle” project and in the “Source code repository” section must be defined the repository URL and its credentials (orange box):</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image5.png" alt="Figure 13 – Source code repository"></p>
<div class="description">Figure 13 – Source code repository</div>
</div>
</div>
<p>In the section &#8220;Build Triggers&#8221; the option &#8220;GitHub hook trigger for GITScm polling&#8221; must be checked:</p>
<div class="image">
<div class="image_inner"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image11.jpg" alt="Figure 14 – “Build triggers” section"></p>
<div class="description">Figure 14 – “Build triggers” section</div>
</div>
</div>
<p>Finally, in the “Build” section, the option “Execute Shell” must be selected and in the command box the scripts that will do the validation of the developed functions:</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image3.jpg" alt="Figure 15 – “Build environment” section"></p>
<div class="description">Figure 15 – “Build environment” section</div>
</div>
</div>
<div class="pblock size_4">&nbsp;</div>
<p>The purpose is to have one validation script for each developed function.</p>
<p>This Python script has a simple set of statements that will call these procedures from a database with some predefined expected results:</p>
<div>
<div id="highlighter_163633" class="syntaxhighlighter  python">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
<div class="line number17 index16 alt2">17</div>
<div class="line number18 index17 alt1">18</div>
<div class="line number19 index18 alt2">19</div>
<div class="line number20 index19 alt1">20</div>
<div class="line number21 index20 alt2">21</div>
<div class="line number22 index21 alt1">22</div>
<div class="line number23 index22 alt2">23</div>
<div class="line number24 index23 alt1">24</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="python comments">#!/usr/bin/python</code></div>
<div class="line number2 index1 alt1"><code class="python keyword">import</code> <code class="python plain">psycopg2</code></div>
<div class="line number3 index2 alt2">&nbsp;</div>
<div class="line number4 index3 alt1"><code class="python plain">con </code><code class="python keyword">=</code> <code class="python plain">psycopg2.connect(database</code><code class="python keyword">=</code><code class="python string">"db_deploy"</code><code class="python plain">, user</code><code class="python keyword">=</code><code class="python string">"postgres"</code><code class="python plain">, password</code><code class="python keyword">=</code><code class="python string">"postgres10"</code><code class="python plain">, host</code><code class="python keyword">=</code><code class="python string">"localhost"</code><code class="python plain">, port</code><code class="python keyword">=</code><code class="python string">"5432"</code><code class="python plain">)</code></div>
<div class="line number5 index4 alt2"><code class="python plain">cur </code><code class="python keyword">=</code> <code class="python plain">con.cursor() </code></div>
<div class="line number6 index5 alt1">&nbsp;</div>
<div class="line number7 index6 alt2"><code class="python plain">email_list </code><code class="python keyword">=</code> <code class="python plain">{ </code><code class="python string">'max1450@mail.com'</code> <code class="python plain">: </code><code class="python color1">True</code><code class="python plain">,</code></div>
<div class="line number8 index7 alt1"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python string">'tintinmail.com'</code>&nbsp;&nbsp; <code class="python plain">:&nbsp; </code><code class="python color1">False</code><code class="python plain">,</code></div>
<div class="line number9 index8 alt2"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python string">'.@severalnines'</code>&nbsp;&nbsp; <code class="python plain">: </code><code class="python color1">False</code><code class="python plain">,</code></div>
<div class="line number10 index9 alt1"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python string">'director#mail.com'</code><code class="python plain">: </code><code class="python color1">False</code><code class="python plain">,</code></div>
<div class="line number11 index10 alt2"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python string">'1942@xmail.com'</code>&nbsp;&nbsp; <code class="python plain">: </code><code class="python color1">True</code></div>
<div class="line number12 index11 alt1"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python plain">}</code></div>
<div class="line number13 index12 alt2"><code class="python plain">result_msg</code><code class="python keyword">=</code> <code class="python string">"f_IsValidEmail -&gt; OK"</code></div>
<div class="line number14 index13 alt1">&nbsp;</div>
<div class="line number15 index14 alt2"><code class="python keyword">for</code> <code class="python plain">key </code><code class="python keyword">in</code> <code class="python plain">email_list:</code></div>
<div class="line number16 index15 alt1"><code class="python spaces">&nbsp;&nbsp;</code><code class="python plain">cur.callproc(</code><code class="python string">'f_IsValidEmail'</code><code class="python plain">, (key,))</code></div>
<div class="line number17 index16 alt2"><code class="python spaces">&nbsp;&nbsp;</code><code class="python plain">row </code><code class="python keyword">=</code> <code class="python plain">cur.fetchone()</code></div>
<div class="line number18 index17 alt1"><code class="python spaces">&nbsp;&nbsp;</code><code class="python keyword">if</code> <code class="python plain">email_list[key]!</code><code class="python keyword">=</code><code class="python plain">row[</code><code class="python value">0</code><code class="python plain">]:</code></div>
<div class="line number19 index18 alt2"><code class="python spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="python plain">result_msg</code><code class="python keyword">=</code> <code class="python string">"f_IsValidEmail -&gt; Nok"</code></div>
<div class="line number20 index19 alt1">&nbsp;</div>
<div class="line number21 index20 alt2"><code class="python functions">print</code> <code class="python plain">result_msg</code></div>
<div class="line number22 index21 alt1">&nbsp;</div>
<div class="line number23 index22 alt2"><code class="python plain">cur.close()</code></div>
<div class="line number24 index23 alt1"><code class="python plain">con.close()</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This script will test the presented&nbsp;<em>PLpg/SQL</em>&nbsp;or&nbsp;<em>plperlu</em>&nbsp;functions and it will be executed after each commit in the code repository in order to avoid regressions on the developments.</p>
<p>Once this job build is executed, the log executions could be verified.</p>
<div class="image">
<div class="image_inner image_zoom"><img decoding="async" src="https://severalnines.com/sites/default/files/blog/node_5608/image1.jpg" alt="Figure 16 – “Console Output”"></p>
<div class="description">Figure 16 – “Console Output”</div>
</div>
</div>
<p>This option presents the final status: SUCCESS or FAILURE, the workspace, the executed files/script, the created temporary files and the error messages (for the failure ones)!</p>
<h2>Conclusion</h2>
<p>In summary, Jenkins is known as a great tool for Continuous Integration (CI) and Continuous Delivery (CD), however, it can be used for various functionalities like,</p>
<ul>
<li>Scheduling tasks</li>
<li>Execution of scripts</li>
<li>Monitoring Processes</li>
</ul>
<p>For all of these purposes on each execution (Build on Jenkins vocabulary) it can be analyzed the logs and elapsed time.</p>
<p>Due to a large number of available plugins it could avoid some developments with a specific aim, probably there is a plugin that does exactly what you’re looking for, it’s just a matter of searching the&nbsp;<a href="https://updates.jenkins.io/" target="_blank" rel="nofollow noopener noreferrer">update center</a>&nbsp;or Manage Jenkins&gt;&gt;Manage Plugins inside the web application.</p>
<p>The post <a href="http://kostacipo.stream/automating-postgresql-daily-tasks-using-jenkins/">Automating PostgreSQL Daily Tasks Using Jenkins</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://kostacipo.stream/automating-postgresql-daily-tasks-using-jenkins/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to backup a PostgreSQL database using Docker</title>
		<link>http://kostacipo.stream/how-to-backup-a-postgresql-database-using-docker/</link>
					<comments>http://kostacipo.stream/how-to-backup-a-postgresql-database-using-docker/#respond</comments>
		
		<dc:creator><![CDATA[Majordomo]]></dc:creator>
		<pubDate>Fri, 31 Jan 2020 10:46:11 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[postgresql]]></category>
		<guid isPermaLink="false">http://www.kostacipo.stream/?p=1723</guid>

					<description><![CDATA[<p>&#160; Before you begin In this tutorial, we’ll learn how to&#160;backup&#160;a&#160;PostgreSQL&#160;database. A Linux machine and&#160;Docker&#160;will be required to follow this tutorial. Backup a PostgreSQL local or remote database Command to backup a local or remote PostgreSQL database using Docker: $ docker run -i postgres /usr/bin/pg_dump \ -h [POSTGRESQL_HOST] \ -U [POSTGRESQL_USER] [POSTGRESQL_DATABASE] &#62; backup.sql Command [&#8230;]</p>
<p>The post <a href="http://kostacipo.stream/how-to-backup-a-postgresql-database-using-docker/">How to backup a PostgreSQL database using Docker</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<h3 id="before-you-begin">Before you begin</h3>
<p>In this tutorial, we’ll learn how to&nbsp;backup&nbsp;a&nbsp;PostgreSQL&nbsp;database. A Linux machine and&nbsp;Docker&nbsp;will be required to follow this tutorial.</p>
<h3 id="backup-a-postgresql-local-or-remote-database">Backup a PostgreSQL local or remote database</h3>
<p>Command to backup a local or remote PostgreSQL database using Docker:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker run <span class="nt">-i</span> postgres /usr/bin/pg_dump <span class="se">\</span>
  <span class="nt">-h</span> <span class="o">[</span>POSTGRESQL_HOST] <span class="se">\</span>
  <span class="nt">-U</span> <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] <span class="o">&gt;</span> backup.sql
</code></pre>
</div>
</div>
<p>Command to backup multiple PostgreSQL databases using Docker:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker run <span class="nt">-i</span> postgres /usr/bin/pg_dumpall <span class="se">\</span>
  <span class="nt">-h</span> <span class="o">[</span>POSTGRESQL_HOST] <span class="se">\</span>
  <span class="nt">-U</span> <span class="o">[</span>POSTGRESQL_USER] <span class="o">&gt;</span> backup.sql
</code></pre>
</div>
</div>
<p>Command to backup a local or remote PostgreSQL database using Docker with compression (using gzip):</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker run <span class="nt">-i</span> postgres /usr/bin/pg_dump <span class="se">\</span>
  <span class="nt">-h</span> <span class="o">[</span>POSTGRESQL_HOST] <span class="se">\</span>
  <span class="nt">-U</span> <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] | <span class="nb">gzip</span> <span class="nt">-9</span> <span class="o">&gt;</span> backup.sql.gz
</code></pre>
</div>
</div>
<p>Same command below but providing PostgreSQL password as environment variable:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker run <span class="nt">-i</span> <span class="nt">-e</span> <span class="nv">PGPASSWORD</span><span class="o">=[</span>POSTGRESQL_PASSWORD] postgres /usr/bin/pg_dump <span class="se">\</span>
  <span class="nt">-h</span> <span class="o">[</span>POSTGRESQL_HOST] <span class="se">\</span>
  <span class="nt">-U</span> <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] | <span class="nb">gzip</span> <span class="nt">-9</span> <span class="o">&gt;</span> backup.sql.gz
</code></pre>
</div>
</div>
<h3 id="backup-a-containerized-postgresql-database">Backup a containerized PostgreSQL database</h3>
<p>Command to backup a containerized PostgreSQL database creating a compressed file using Docker and gzip:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker <span class="nb">exec</span> <span class="o">[</span>POSTGRESQL_CONTAINER] /usr/bin/pg_dump <span class="se">\</span>
  <span class="nt">-U</span> <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] | <span class="nb">gzip</span> <span class="nt">-9</span> <span class="o">&gt;</span> backup.sql.gz
</code></pre>
</div>
</div>
<p>Same command below but setting PostgreSQL password environment variable to existing container:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker <span class="nb">exec</span> <span class="o">[</span>POSTGRESQL_CONTAINER] /bin/bash <span class="se">\</span>
  <span class="nt">-c</span> <span class="s2">"export PGPASSWORD=[POSTGRESQL_PASSWORD] </span><span class="se">\</span><span class="s2">
      &amp;&amp; /usr/bin/pg_dump -U [POSTGRESQL_USER] [POSTGRESQL_DATABASE]"</span> <span class="se">\</span>
  | <span class="nb">gzip</span> <span class="nt">-9</span> <span class="o">&gt;</span> backup.sql.gz
</code></pre>
</div>
</div>
<h3 id="bonus-track-how-to-dump-a-portion-of-a-table">Bonus track: How to dump a portion of a table?</h3>
<p>Using Postgres functions you can dump all databases, a database, a schema, only schema data or even a single table dataset. These are some of the available options:</p>
<p><code class="language-bash highlighter-rouge">pg_dumpall</code>: Retrieves all databases.</p>
<p><code class="language-bash highlighter-rouge">pg_dump</code>: Retrieves specific database.</p>
<p><code class="language-bash highlighter-rouge">pg_dump&nbsp;<span class="nt">--schema-only</span>&nbsp;DATABASE_NAME</code>: Retrieves only schema/structure of a database.</p>
<p><code class="language-bash highlighter-rouge">pg_dump&nbsp;<span class="nt">--table</span>&nbsp;TABLE_NAME</code>: Dumps the content of the table TABLE_A.</p>
<p><code class="language-bash highlighter-rouge">COPY</code>: Retrieves data from a table and outputs it to a file or stdout. E.g.:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker <span class="nb">exec</span> <span class="nt">-i</span> <span class="o">[</span>POSTGRESQL_CONTAINER] /usr/bin/psql <span class="nt">-U</span> <span class="se">\</span>
  <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] <span class="se">\</span>
  <span class="nt">-c</span> <span class="s2">"COPY (SELECT * FROM [TABLE_NAME] order by time desc limit 1000)
  TO 'dest/folder/filename.txt';"</span>
</code></pre>
</div>
</div>
<p>In previous example we are retrieving last 1000 records from a table&nbsp;<em>TABLE_NAME</em>&nbsp;sorted by&nbsp;<em>time</em>&nbsp;and saving it to a text file&nbsp;<em>dest/folder/filename.txt</em>.</p>
<p>Lately this data file can be restored using the same command&nbsp;<code class="language-bash highlighter-rouge">COPY</code>&nbsp;as the following example does:</p>
<div class="language-bash highlighter-rouge">
<div class="highlight">
<pre class="highlight"><code><span class="nv">$ </span>docker <span class="nb">exec</span> <span class="nt">-i</span> <span class="o">[</span>POSTGRESQL_CONTAINER] /usr/bin/psql <span class="nt">-U</span> <span class="se">\</span>
  <span class="o">[</span>POSTGRESQL_USER] <span class="o">[</span>POSTGRESQL_DATABASE] <span class="se">\</span>
  <span class="nt">-c</span> <span class="s2">"COPY [TABLE_NAME] from 'dest/folder/filename.txt' WITH (FORMAT text);"</span>
</code></pre>
</div>
</div>
<p>More info about&nbsp;<code class="language-bash highlighter-rouge">COPY</code>&nbsp;here:&nbsp;<a href="https://www.postgresql.org/docs/10/static/sql-copy.html" target="_blank" rel="noopener noreferrer">https://www.postgresql.org/docs/10/static/sql-copy.html</a></p>
<p>The post <a href="http://kostacipo.stream/how-to-backup-a-postgresql-database-using-docker/">How to backup a PostgreSQL database using Docker</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://kostacipo.stream/how-to-backup-a-postgresql-database-using-docker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How SQL Database Engines Work</title>
		<link>http://kostacipo.stream/how-sql-database-engine-work/</link>
					<comments>http://kostacipo.stream/how-sql-database-engine-work/#respond</comments>
		
		<dc:creator><![CDATA[Majordomo]]></dc:creator>
		<pubDate>Fri, 17 Jan 2020 10:11:33 +0000</pubDate>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">http://www.kostacipo.stream/?p=1659</guid>

					<description><![CDATA[<p>&#160; As a start, I am assuming that you all know what is SQL or you have some experience with SQL or you are using SQL for many years. &#160; I know many people who know so much about SQL and even have created many projects based on SQL and they do not know what [&#8230;]</p>
<p>The post <a href="http://kostacipo.stream/how-sql-database-engine-work/">How SQL Database Engines Work</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<div class="paragraph">As a start, I am assuming that you all know what is SQL or you have some experience with SQL or you are using SQL for many years.</div>
<div>&nbsp;</div>
<div class="paragraph">I know many people who know so much about SQL and even have created many projects based on SQL and they do not know what is the SQL engine or how actually SQL database engine works. It’s obvious no one’s care about the internal working of SQL engine or something because without knowing How SQL works we can still create and access database using any SQL program.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>##Is SQL a Programming or a Query Language?</strong></div>
<div>&nbsp;</div>
<div class="paragraph">With SQL we can interchangeably use the Query and Programming language, many developers refer SQL as a peculiar programming language because SQL engine contain two components compiler and vertical machine the compiler compile the query commands into procedures and the Virtual machine runs that procedures. The concept of SQL engine compiling and executing the SQL query make it a programming language. So if someone says SQL is a programming language then up to some extent he would be considered right.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Why to use SQL if we any programming Language can create a CRUD operations program?</strong></div>
<div>&nbsp;</div>
<div class="paragraph">It’s correct that using any programming language we can create a program which can perform simple CRUD (Create Read Update and Delete) operations, but when it’s come complex queries for that in programming language we have to write hundreds of lines for the equivalent few lines of SQL.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>SQL Overview</strong></div>
<div>&nbsp;</div>
<div class="paragraph">SQL stands for Structured Query Language which could be refer as either a programming or a Query language, the main purpose of SQL to interact with the relational database in which data store in tabular form. SQL can manage a large amount of data, especially if data is written simultaneously and we have many transitions over that data.</div>
<div class="paragraph">When the user uses SQL for data management, there the user gets the ability to perform Create, Retrieve, Update, and Delete data between database. There are various Relational Database Management systems(DBMS) such as MySQL, SQLite, Postgres SQL, etc. and they all provided the same kind of features.</div>
<div class="paragraph">There are some terminologies in Database such as database server, or Database engine, or database management system. In database we can interchangeably use these three terminologies so do not get confused when we say SQL engine or SQL server, they all are same.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>What is a SQL Engine?</strong></div>
<div>&nbsp;</div>
<div class="paragraph">A SQL engine is a kind of software that collect and interprets the SQL commands so the appropriate operations can be performed on the relational database. The objective of SQL engine to create, read, update and delete (CRUD) data from a database.</div>
<div class="paragraph">A SQL engine or a SQL server database engine includes two main components a storage engine and a query processor, these days some modern SQL DBMS contains more than one Storage engines. We have many types of SQL engines and they all have different architecture, but used to perform the same objective which includes CRUD operations on the database and many other features.</div>
<div>&nbsp;</div>
<div class="paragraph">SQL has cover a large part of online market and many enterprises use their SQL systems for Online Transaction Processing and online analytical processing.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>How the SQL Database Engine Works?</strong></div>
<div>&nbsp;</div>
<div class="paragraph">On the surface we all know, the compiler of SQL compiles the query and the virtual machine execute the compiled query. Now let’s talk about how the database engine works.</div>
<div class="paragraph">SQL has many stages on which the process of query compilation and execution take place. Every SQL database engine contain two main components Compiler and Virtual machine to execute the queries. The compiler read the query and convert that query to appropriate byte code, then that byte code evaluated by the virtual machine and a proper response given back to the client.</div>
<div class="paragraph">The Complete Execution of a query is Categories into 3 main stages</div>
<ul>
<li>Compiling (Parsing, Checks, and Semantics)</li>
<li>Binding</li>
<li>Optimising</li>
<li>Executing</li>
</ul>
<div class="paragraph"><strong>Compiling-parsing</strong></div>
<div>&nbsp;</div>
<div class="paragraph">This is a part of compiling process, and in compiling parsing the query statement is tokenized into individual words with appropriate verbiage and clauses.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Compiling-Check-Semantics</strong></div>
<div>&nbsp;</div>
<div class="paragraph">The Compiling Semitics checks the validation of the statement and match it with the system’s catalogue. This Compiling stage validates whether the query is valid or not, it also validates the authority of user to execute the statement.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Compiling-Binding</strong></div>
<div>&nbsp;</div>
<div class="paragraph">It creates the corresponding binary representation for the entered query statement. All the SQL server engines has this compiling state where the byte code get generated.</div>
<div class="paragraph">By this stage of compiling the statement has been compiled and now it will be sent to the database server for the optimization and execution.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Optimising</strong></div>
<div>&nbsp;</div>
<div class="paragraph">It Optimizes the best algorithm for the byte code. This feature is also known as Query Optimizer or Relational Engine.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Executing</strong></div>
<div>&nbsp;</div>
<div class="paragraph">The Virtual machine get the Optimised byte code and execute it.</div>
<div class="paragraph">
<pre><code><strong>SQL STATEMENT --&gt; Parsing --&gt;Binding --&gt; Query Optimization --&gt; Query Execution --&gt;&nbsp;Result</strong></code></pre>
</div>
<div class="paragraph"><strong>Note: </strong>The Parsing Compiling process does not require any permission from database which make it the fastest processing stage of Compiling.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>SQL conversion of Data to Table</strong></div>
<div>&nbsp;</div>
<div class="paragraph">SQL was written in C and it uses a principle of Binary-Tree which make the incoming data to store in Rows and Columns. In binary tree structure we have several branches that keep pointing to the new data element same structure is followed by the SQL database in which data get turned into tables where each column and rows data point to each other.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>SQL Database Engines Work in a Nutshell</strong></div>
<div>&nbsp;</div>
<div class="paragraph">The SQL engine Process the query into multistage. The processing of query can vary from one relational DBMS to another. In the very first stage the query get parse and converted into compatible format such as JASON file, then the another compiling process take place which check the sematic of the parsed file, and in last stage of compilation the parsed file gets converted into corresponding byte code.</div>
<div class="paragraph">The Second step is optimization in which appropriate algorithms such as sorting, searching, etc. applied on the byte code. At last, the virtual machine executes the code and provide the client with the proper result.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>What is a SQL Server Storage Engine?</strong></div>
<div>&nbsp;</div>
<div class="paragraph">It is a software which is used to perform CRUD (Create, Read, Update and Delete) operation between disk and memory.</div>
<div>&nbsp;</div>
<div class="paragraph"><strong>Conclusion</strong></div>
<div>&nbsp;</div>
<div class="paragraph">To create a database environment, we require SQL database engine, and to build the database engine we often use the low-level programming languages such as C++ or C because they give user control over the memory management, which is missing in high-level programming languages, in high-level programming languages memory-management is done automatically by the operating system. SQL engine is cross-platform developers can use different platforms to build some program but all the platform can link with SQL engine for the database facilities.</div>
<p>The post <a href="http://kostacipo.stream/how-sql-database-engine-work/">How SQL Database Engines Work</a> appeared first on <a href="http://kostacipo.stream">Tech Chronicles</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://kostacipo.stream/how-sql-database-engine-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
