<?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>Forem: The Smartbug</title>
    <description>The latest articles on Forem by The Smartbug (@thesmartbug).</description>
    <link>https://forem.com/thesmartbug</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%2F2358685%2F6ba3afb8-eace-44bb-a34d-acd72947919f.png</url>
      <title>Forem: The Smartbug</title>
      <link>https://forem.com/thesmartbug</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thesmartbug"/>
    <language>en</language>
    <item>
      <title>How to self-host Postgres Database on Linux</title>
      <dc:creator>The Smartbug</dc:creator>
      <pubDate>Thu, 07 Nov 2024 21:48:14 +0000</pubDate>
      <link>https://forem.com/thesmartbug/how-to-self-host-postgres-database-on-linux-582m</link>
      <guid>https://forem.com/thesmartbug/how-to-self-host-postgres-database-on-linux-582m</guid>
      <description>&lt;p&gt;Postgres is a performant SQL Database, and packs a ton of features. You can self-host Postgres on any linux machine including a Raspberry Pi. Read on to find out how.&lt;/p&gt;

&lt;h2&gt;
  
  
  PostgresDB
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt;, commonly referred to as Postgres, is a powerful, open-source relational database management system (RDBMS) known for its robustness, extensibility, and standards compliance. It was introduced in 1986 by Michael Stonebraker and his team at the University of California, Berkeley, as part of the POSTGRES project, which aimed to address some of the limitations of existing database systems at the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation behind
&lt;/h2&gt;

&lt;p&gt;The primary motivation behind the creation of PostgreSQL was to extend the ideas of the &lt;a href="https://en.wikipedia.org/wiki/Ingres_(database)" rel="noopener noreferrer"&gt;&lt;strong&gt;Ingres&lt;/strong&gt;&lt;/a&gt; database project, also led by &lt;em&gt;Stonebraker&lt;/em&gt;, and to support a wider variety of data types and complex queries. The name "POSTGRES" originally stood for "&lt;em&gt;POST Ingres,&lt;/em&gt;" reflecting its goal of building upon the foundational concepts of its predecessor.&lt;/p&gt;

&lt;p&gt;PostgreSQL was designed to support advanced data types, indexing techniques, and a rich set of features like transactional integrity, concurrency control, and complex querying capabilities. One of the core ideas was to provide a database system that could be easily extended by users, allowing them to define new data types, operators, and functions to suit their specific needs.&lt;/p&gt;

&lt;p&gt;Since its initial release, PostgreSQL has evolved into a feature-rich and highly reliable RDBMS used by organizations worldwide. Its adherence to SQL standards, combined with its extensibility and strong community support, has made PostgreSQL a preferred choice for a wide range of applications, from simple web services to complex, data-intensive applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self-host Postgres Setup
&lt;/h2&gt;

&lt;p&gt;At the time of writing this blog post, the latest postgres version is 14. So, to setup a PostgreSQL instance on your machine, (without SSL), use the following docker compose file&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.9'&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:14-alpine3.18&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="c1"&gt;# set shared memory limit when using docker-compose&lt;/span&gt;
    &lt;span class="na"&gt;shm_size&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;128mb&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;CUSTOM_PORT&amp;gt;:5432&lt;/span&gt;

    &lt;span class="c1"&gt;# You can set additional values&lt;/span&gt;
    &lt;span class="c1"&gt;#volumes:&lt;/span&gt;
    &lt;span class="c1"&gt;#  - type: tmpfs&lt;/span&gt;
    &lt;span class="c1"&gt;#    target: /dev/shm&lt;/span&gt;
    &lt;span class="c1"&gt;#    tmpfs:&lt;/span&gt;
    &lt;span class="c1"&gt;#      size: 134217728 # Calculated like this: 128*2^20 bytes = 128Mb&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;&amp;lt;DATABASE_USERNAME&amp;gt;&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;&amp;lt;DATABASE_PASSWORD&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add SSL support for PostgresDB
&lt;/h2&gt;

&lt;p&gt;To enable the SSL certificate, we can take multiple routes and all are equally safe.&lt;/p&gt;

&lt;p&gt;I personally prefer the first approach, someone else may like other approach, its subjective.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method : 1
&lt;/h3&gt;

&lt;p&gt;One way to setup SSL for the PostgresDB is to create /acquire SSL certificates and simply add them using the Environment variables. There are many certificate authorities like - &lt;a href="https://www.digicert.com/?ref=thesmartbug.com" rel="noopener noreferrer"&gt;DigiCert&lt;/a&gt;, &lt;a href="https://www.geotrust.com/?ref=thesmartbug.com" rel="noopener noreferrer"&gt;GeoTrust&lt;/a&gt;, or perhaps &lt;a href="https://www.globalsign.com/en?ref=thesmartbug.com" rel="noopener noreferrer"&gt;GlobalSign&lt;/a&gt; from where one can purchase a secure SSL certificates. If you prefer free SSL certificates, you can also generate an SSL certificate signed by &lt;a href="https://letsencrypt.org/?ref=thesmartbug.com" rel="noopener noreferrer"&gt;Let's Encrypt&lt;/a&gt;. Otherwise, you can use &lt;a href="https://openssl-library.org/" rel="noopener noreferrer"&gt;OpenSSL&lt;/a&gt; to create a self-signed certificate as well, but these are not recommended for production use. But for development, it works fine.&lt;/p&gt;

&lt;p&gt;Once you have the certificates, simply use the environment variables &lt;code&gt;POSTGRES_SSL_CERT_FILE&lt;/code&gt; and &lt;code&gt;POSTGRES_SSL_KEY_FILE&lt;/code&gt; to load them while deploying the postgres instance using docker-compose as shown below&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.9'&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:14-alpine3.18&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;shm_size&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;128mb&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;CUSTOM_PORT&amp;gt;:5432&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;&amp;lt;DATABASE_USERNAME&amp;gt;&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;&amp;lt;DATABASE_PASSWORD&amp;gt;&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_SSL_CERT_FILE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/path/to/ssl/certificate.crt&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_SSL_KEY_FILE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/path/to/ssl/key.key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method : 2
&lt;/h3&gt;

&lt;p&gt;One of the simplest option is to use the self-signed SSL certs which every debian distribution includes. Meaning, your &lt;code&gt;docker-compose&lt;/code&gt; config could look like this&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="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.8"&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;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="s"&gt;-c ssl=on &lt;/span&gt;
      &lt;span class="s"&gt;-c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem &lt;/span&gt;
      &lt;span class="s"&gt;-c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key&lt;/span&gt;
&lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="c1"&gt;# rest of the config&lt;/span&gt;
&lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method : 3
&lt;/h3&gt;

&lt;p&gt;Another method can be to use the SSL certificates obtained from a renowned SSL certificate provider like DigiCert, a Certificate Authority for more than two decades now.&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="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.8"&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;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="s"&gt;-c ssl=on &lt;/span&gt;
      &lt;span class="s"&gt;-c ssl_cert_file=/path/to/certificate.pem &lt;/span&gt;
      &lt;span class="s"&gt;-c ssl_key_file=/path/to/privkey.key&lt;/span&gt;
&lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="c1"&gt;# rest of the config&lt;/span&gt;
&lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You can use your own self-hosted instance of PostgresDB without any restriction and allocate as much data as you like. It is an easy to use, and easy to setup Database. It only takes 5 minutes of your time if you follow this guide.&lt;/p&gt;

&lt;p&gt;Feel free to post in the comments below if something is unclear or if you've any suggestion. I would be very glad to read your comments.&lt;/p&gt;

&lt;p&gt;Also, any feedback is most welcome.&lt;/p&gt;

&lt;p&gt;Feel free to checkout my other Blog post on &lt;a href="https://thesmartbug.com/blog/some-commonly-used-postgres-commands/" rel="noopener noreferrer"&gt;Some commonly used Postgres commands&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Original Article published at &lt;a href="https://thesmartbug.com/blog/how-to-self-host-postgres-database-on-linux/" rel="noopener noreferrer"&gt;The SmartBug&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>raspberrypi</category>
      <category>docker</category>
    </item>
    <item>
      <title>How to self-host MongoDB on a Raspberry Pi 4</title>
      <dc:creator>The Smartbug</dc:creator>
      <pubDate>Wed, 06 Nov 2024 02:33:16 +0000</pubDate>
      <link>https://forem.com/thesmartbug/how-to-self-host-mongodb-on-a-raspberry-pi-4-3fpk</link>
      <guid>https://forem.com/thesmartbug/how-to-self-host-mongodb-on-a-raspberry-pi-4-3fpk</guid>
      <description>&lt;h2&gt;
  
  
  MongoDB Server
&lt;/h2&gt;

&lt;p&gt;MongoDB is an open-source, NoSQL database management system designed to handle large volumes of data and provide high performance, scalability, and flexibility. Introduced in 2009 by Dwight Merriman, Eliot Horowitz, and Kevin Ryan, the co-founders of the company 10gen (later renamed MongoDB, Inc.), MongoDB emerged as a solution to the limitations and challenges posed by traditional relational databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation behind
&lt;/h2&gt;

&lt;p&gt;The primary motivation behind MongoDB's creation was the need to manage and store the vast amounts of unstructured and semi-structured data generated by modern applications. Traditional relational databases, which use fixed schemas and tables, struggled to efficiently handle the diverse and evolving data structures typical of contemporary web applications, big data, and real-time analytics.&lt;/p&gt;

&lt;p&gt;MongoDB uses a document-oriented model, storing data in flexible, JSON-like documents. This schema-less design allows developers to easily adapt to changing data requirements without costly schema migrations. Additionally, MongoDB's architecture supports horizontal scaling through sharding, enabling it to manage large-scale, distributed data across multiple servers seamlessly.&lt;/p&gt;

&lt;p&gt;By providing a more dynamic and scalable approach to data management, MongoDB has become a popular choice for organizations looking to build applications that require real-time processing, complex querying, and rapid iteration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self-host MongoDB setup
&lt;/h2&gt;

&lt;p&gt;To setup instance of the MongoDB server on a Raspi4, we can use the following docker compose file&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;2'&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;mongodb&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# For Development / Testing&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;mongodb/mongodb-community-server:4.4.3-ubuntu2004&lt;/span&gt;

    &lt;span class="c1"&gt;# For Production Deployment&lt;/span&gt;
    &lt;span class="c1"&gt;# image: mongodb/mongodb-enterprise-server:4.4.3-ubuntu2004&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;MONGODB_INITDB_ROOT_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;DB_USERNAME&amp;gt;&lt;/span&gt;
      &lt;span class="na"&gt;MONGODB_INITDB_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;DB_PASSWORD&amp;gt;&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;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;27017:27017&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;mongodb_data:/data/db&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongodb_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;driver&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;local&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is extremely important to note that the &lt;code&gt;latest&lt;/code&gt; versions of the docker images will NOT work on the Raspberry Pi 4. It is because the architecture of Raspi is &lt;code&gt;arm64/v8&lt;/code&gt; and there are many new additions in the architecute &lt;code&gt;arm64/v8.2&lt;/code&gt; that are absent on the Raspberry pi.&lt;br&gt;
More details &lt;a href="https://github.com/docker-library/mongo/issues/485#issuecomment-970864306" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://en.wikichip.org/wiki/arm/armv8#ARMv8_Extensions_and_Processor_Features" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Save the above docker-compose config in some file (say &lt;em&gt;mongo-docker-compose.yaml&lt;/em&gt;), and run the following cmd&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose &lt;span class="nt"&gt;-d&lt;/span&gt; mongo-docker-compose.yaml up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-d&lt;/code&gt; flag instructs the command to run in a detached mode, in background.&lt;br&gt;
After testing many different Docker images, and configurations, finally the above &lt;code&gt;docker-compose&lt;/code&gt; file has been prepared. This has been proven to work on raspberry pi 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You can use your own self-hosted instance of MongoDB without any restriction and allocate as much data as you like. It is an easy to use, and easy to setup Database. It only takes 5 minutes of your time if you follow this guide.&lt;/p&gt;

&lt;p&gt;Feel free to post in the comments below if something is unclear or if you've any suggestion. I would be very glad to read your comments.&lt;/p&gt;

&lt;p&gt;Also, any feedback is most welcome&lt;/p&gt;




&lt;p&gt;Original Article published at &lt;a href="https://thesmartbug.com/blog/how-to-self-host-mongodb-on-a-raspberry-pi-4/" rel="noopener noreferrer"&gt;The SmartBug&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>raspberrypi</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
