<?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: Lewis Wood</title>
    <description>The latest articles on DEV Community by Lewis Wood (@lewislwood).</description>
    <link>https://dev.to/lewislwood</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%2F1079144%2F23da36a5-4766-4d36-83b8-1c7f9a9e7003.png</url>
      <title>DEV Community: Lewis Wood</title>
      <link>https://dev.to/lewislwood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lewislwood"/>
    <language>en</language>
    <item>
      <title>Postgresql, PSQL prompt, Persistent, Backup &amp; Restore Data,</title>
      <dc:creator>Lewis Wood</dc:creator>
      <pubDate>Sun, 28 May 2023 17:23:53 +0000</pubDate>
      <link>https://dev.to/lewislwood/postgresql-psql-prompt-persistent-backup-restore-data-4g9h</link>
      <guid>https://dev.to/lewislwood/postgresql-psql-prompt-persistent-backup-restore-data-4g9h</guid>
      <description>&lt;p&gt;&lt;a href="http://github.com/lewislwood/postgresql-persistent-connect"&gt;Repo postgresql-persistent-connect&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This docker containerized server is up and running in seconds.  Has the features any good developer demands:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Quick and Easy to setup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Persistent data.  Data sticks around even after container is destoyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to Backup and Resotre&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Runs psql command line and you can quickly develop and test your sql.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This little simple javascript console app (index.js). Illustrates an easy way to connect to this new database. Even accepts a parameter for a psql file, that it will run against the server.&lt;/p&gt;

&lt;p&gt;The Postgresql server is created from one file (docker-compose.yml). This yml file will run the server with a container name for easy reference (instead of that long container id).  It maps a volume to a data folder in the current directory. Warning Docker creates this file, so do not create it yourself.  Also maps a port 5432 from inside to 5432 in your local &lt;br&gt;
computer.  You change the port number on the left side to have your apps access it from  different local port. This can be handy if you are running 3 local istes locally with 3 different postgresql servers.  Example - 5451:5432  on site 2 5452:5432, ... &lt;/p&gt;

&lt;p&gt;you can choose your own container name, port.  local-postgres is just the name for this starter. I personally will be using shorter names without the "-" for myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Server
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $ docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command will tell dockerto download the the latest postgresql server image file. Then it will create a running containter named local-postgres on port 5432.  That's it!&lt;/p&gt;

&lt;p&gt;To stop the docker container:&lt;br&gt;
 $ docker-compose stop&lt;/p&gt;

&lt;p&gt;This will keep the container for you to use again at a later time.&lt;/p&gt;

&lt;p&gt;To estroy the container .&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $ docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can also destroy it from anywhere on your computer by removing it by container name:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker rm local-postgres -f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The -f forces it to be removed, even if it is running.&lt;/p&gt;

&lt;p&gt;Do not worry regardless the data is still present and can be spun up again with all the data contined in the Postgresql server database.:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Sample psql files
&lt;/h2&gt;

&lt;p&gt;Inside the psql folder you will find a few psql files.  You can run any of these as follows.  Keep in mind the database has only test_db and no tables when first created.&lt;/p&gt;

&lt;p&gt;You can add data in multiple ways. I present 3 ways  for your convenience.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;run enclosed app with the provided psql/*.psql files. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;psql to run the sql files and/or  manual queries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;restore //One backup (dump*.psql) file is provided.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Console app running it.
&lt;/h3&gt;

&lt;p&gt;This console app can add the data and verify your postgresql server is running.&lt;/p&gt;

&lt;p&gt;A one ime step is to install the program dependencies.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command will install all depencies for this console app. &lt;/p&gt;

&lt;p&gt;Once installed and postgres server is runing. You can run this console app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This runs the app with no parameters and uses the default query.  { SELECT NOW()}. This just displays the server time. This proves the server is up and running.&lt;/p&gt;

&lt;p&gt;Now you are ready to run queries with this console app. Now in order to input parameters into the console app you will have a different command to run.:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index.js [psql file]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Adding data via console app
&lt;/h2&gt;

&lt;p&gt;I provided numerous psql files in the psql folder for you to test. Feel free to write your own.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index.js psql/adddata.psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above will create the users table if need be and add users to the table. Run the users query to verify they were added.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index users.psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Adding data via PSQL prompt
&lt;/h2&gt;

&lt;p&gt;This way you can run psql queries I wrote or you can manually type in. First thing you will have to do is start the docker container command shell prompt:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker -it local-postgres bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command opens the conatiner and places you inside the shell.  Bash is the default shell for linux based containers. &lt;/p&gt;

&lt;p&gt;Now you can run other commands, and in our case we want to start psql prompt.&lt;br&gt;
    $ psql -Uadmin -d test_db&lt;/p&gt;

&lt;p&gt;The above psql will open the psql prompt for the test_db that our yml file created earlier.   We use the capital letter "U" parameter and  enter our user id "admin".  If we had named the db in the yml admin, this would not be neccessary, since psql opens the users database as a default.&lt;/p&gt;

&lt;p&gt;Now you can manually type in psql commands for example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test_db# SELECT NOW();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Above query displays time and date.  Notice all querys must end with ";".&lt;/p&gt;

&lt;p&gt;Now if you want to run the psql files I provide ou will need to do the following:&lt;/p&gt;

&lt;p&gt;Open another terminal and type int ehe following:&lt;br&gt;
     $ docker cp *.psql local-postgres:.&lt;/p&gt;

&lt;p&gt;The above will copy all your psql files into your container.  Now you can run the queries from the psql prompt.  You may have wanted to create a create sub folder for organization sake. In the earlier docker bash command shell. &lt;/p&gt;

&lt;p&gt;Now enter the psql quiry in the other terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; test_db# \i adddata.psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will run the psql query found in the container.  The \i means input file. No need for simicolon for the "\" commands.&lt;/p&gt;

&lt;p&gt;##  Restoring from a backup file&lt;/p&gt;

&lt;p&gt;One dump*.sql file is provided for you to restore from. You can use this to restore the test_db to the state I backed it up.  You will find it has users, but no cities. This was done purposely, so that you can see and verify it as well. You will find  the command in the backup.txt and the dump*.sql files in the backup folder.  The backup.txt has the command below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat dump.psql | docker exec -i local-postgres psql -d test_db -U admin 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now to run the above command you will need to be in the backup folder or adjust the command appropiately.  Also you will need to ren the dump_date-time.psql file to dump.psql.  I did this for simpicity sake. Here is another approach you may consider and may be faster for large databases restore.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker exec -it local-postgres bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
root$ mkdir psql
 **** Another terminal

     $docker cp dump.psql local-postgres:psql/.

    root$ cd psql
    root$ psql -d test_db -U admin  
test_db# \i dump.psql

Now you are running the restory command from inside the container without piping the file to the psql via cat command.

You create backups with the following command found in the backup.txt file as well.

    docker exec -i local-postgres pg_dumpall -c -U admin &amp;gt; dump_`date +%d-%m-%Y"_"%H_%M`.psql

## Resources and references

You can find my repository at the folowing:
[Repo postgresql-persistent-connect](http://github.com/lewislwood/postgresql-persistent-connect)

My inspiration for this repository can be found in the old folder. A simpler and even easier format to understand. Also a much better writer than myself.
[run-postgresql-with-docker-locally-and-connect](https://dev.to/mohsenkamrani/run-postgresql-with-docker-locally-and-connect-to-it-with-nodejs-451g#main-content)

I hope you found this repository and article  a good resources.  You can find my website at:

[Blind Heroes](http://blindheroes.org)

This is currently in WordPress and will soon be switching to Express, which I prefer.  Sty tuned.











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

&lt;/div&gt;

</description>
      <category>postgres</category>
      <category>docker</category>
      <category>persistent</category>
      <category>psql</category>
    </item>
  </channel>
</rss>
