<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Vivek Shukla - Selfhost</title>
    <subtitle>I am a Software Engineer from India. I am currently building Daestro. I work in Rust, Javascript and Python.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://vshukla.com/tags/selfhost/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://vshukla.com/tags/selfhost/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-09-08T00:00:00+00:00</updated>
    <id>https://vshukla.com/tags/selfhost/atom.xml</id>
    <entry xml:lang="en">
        <title>Setup Redis TLS With Self-signed Certificate</title>
        <published>2025-09-08T00:00:00+00:00</published>
        <updated>2025-09-08T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/setup-redis-tls-with-self-signed-certs/"/>
        <id>https://vshukla.com/tech/setup-redis-tls-with-self-signed-certs/</id>
        <summary type="html">Brief guide on how to setup Redis TLS using Self-signed certificate.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/setup-redis-tls-with-self-signed-certs/">&lt;blockquote&gt;
&lt;p&gt;This method is only recommended when connecting to redis server via local network.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;generate-server-certificates&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#generate-server-certificates&quot; aria-label=&quot;Anchor link for: generate-server-certificates&quot;&gt;🔗&lt;&#x2F;a&gt;Generate Server Certificates&lt;&#x2F;h2&gt;
&lt;p&gt;Generate Cert and Key file for self signed TLS, then move both these files to redis directory and set the proper permissions. Delete the Key file and copy the cert file to the client machine which will connect to the redis-server.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ~&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;openssl&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; req&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -x509 -nodes -days&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3650&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -newkey&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; rsa:2048&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-character&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-other&quot;&gt;  -keyout&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; redis_tls.key&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -out&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; redis_tls.crt&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-character&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-other&quot;&gt;  -subj&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;&#x2F;CN=YourOrg&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; cp redis_tls.key redis_tls.crt &#x2F;etc&#x2F;redis&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; rm&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; redis_tls.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; chmod&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 600&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;redis&#x2F;redis_tls.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; chmod&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 644&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;redis&#x2F;redis_tls.crt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; chown redis:redis &#x2F;etc&#x2F;redis&#x2F;redis_tls.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; chown redis:redis &#x2F;etc&#x2F;redis&#x2F;redis_tls.crt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;update-redis-configuration-file&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#update-redis-configuration-file&quot; aria-label=&quot;Anchor link for: update-redis-configuration-file&quot;&gt;🔗&lt;&#x2F;a&gt;Update Redis Configuration File&lt;&#x2F;h2&gt;
&lt;p&gt;This command will append the value to &lt;code&gt;redis.conf&lt;&#x2F;code&gt; (redis configuration file). Make sure to update the password.&lt;&#x2F;p&gt;
&lt;p&gt;It will disable the non-tls support and enable the tls connection at port &lt;code&gt;6379&lt;&#x2F;code&gt; with previously generated key and cert files.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; tee&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -a&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;redis&#x2F;redis.conf&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; EOF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;bind * -::*&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;port 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tls-port 6379&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tls-cert-file &#x2F;etc&#x2F;redis&#x2F;redis_tls.crt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tls-key-file &#x2F;etc&#x2F;redis&#x2F;redis_tls.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tls-auth-clients no&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tls-prefer-server-ciphers yes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;requirepass YOUR_PASSWORD_HERE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;EOF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Restart and enable redis&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; systemctl restart redis-server&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; systemctl enable redis-server&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Check status with&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; systemctl status redis-server&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If any error then check journal (log)&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; journalctl&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -u&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; redis-server&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -f&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;add-redis-certificate-to-trusted-ca-store-in-client-machine&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#add-redis-certificate-to-trusted-ca-store-in-client-machine&quot; aria-label=&quot;Anchor link for: add-redis-certificate-to-trusted-ca-store-in-client-machine&quot;&gt;🔗&lt;&#x2F;a&gt;Add Redis Certificate to Trusted CA Store in Client Machine&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Copy your certificate to CA certificates directory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; mv redis_tls.crt &#x2F;usr&#x2F;local&#x2F;share&#x2F;ca-certificates&#x2F;redis_tls.crt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Update CA certificates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; update-ca-certificates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;redis-connection-url&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#redis-connection-url&quot; aria-label=&quot;Anchor link for: redis-connection-url&quot;&gt;🔗&lt;&#x2F;a&gt;Redis Connection URL&lt;&#x2F;h2&gt;
&lt;p&gt;Since we want to connect to redis using TLS connection, so the protocol in the url will be &lt;code&gt;rediss&lt;&#x2F;code&gt; (with double “ss”).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;rediss:&#x2F;&#x2F;:&amp;lt;your_password_here&amp;gt;@&amp;lt;redis_server_ip&amp;gt;:6379&#x2F;0#insecure&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
    </entry>
    <entry xml:lang="en">
        <title>Self Host PostgreSQL With TLS</title>
        <published>2025-02-03T00:00:00+00:00</published>
        <updated>2025-09-09T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/self-host-postgresql-with-tls/"/>
        <id>https://vshukla.com/tech/self-host-postgresql-with-tls/</id>
        <summary type="html">Complete tutorial on how to self-host PostgreSQL instance for production usage with TLS.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/self-host-postgresql-with-tls/">&lt;p&gt;In this tutorial we will go through how to self-host PostgreSQL Database and setup TLS to securely connect.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-postgresql&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#installing-postgresql&quot; aria-label=&quot;Anchor link for: installing-postgresql&quot;&gt;🔗&lt;&#x2F;a&gt;Installing PostgreSQL&lt;&#x2F;h2&gt;
&lt;p&gt;Postgres APT repository configuration:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; apt install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -y&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgresql-common&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;usr&#x2F;share&#x2F;postgresql-common&#x2F;pgdg&#x2F;apt.postgresql.org.sh&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Install PostgreSQL 16&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; apt install postgresql-16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After installing PostgreSQL, it creates a new user &lt;code&gt;postgres&lt;&#x2F;code&gt;, which is the superuser of the database. We can switch to &lt;code&gt;postgres&lt;&#x2F;code&gt; user in ubuntu to access postgresql command prompt:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -i -u&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now to create new user with passowrd and with privilege to create new db. It will ask you to enter your password, which will not be logged into history hence this is the recommended way to set password.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;createuser&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; your_db_user&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --pwprompt --createdb&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can exit from &lt;code&gt;postgres&lt;&#x2F;code&gt; account&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;exit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We will add new user in our Ubuntu with the same name as the new user we created in postgresql. (Optional) Add &lt;code&gt;-m&lt;&#x2F;code&gt; to create home directory for the user.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; useradd&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -s&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;bin&#x2F;bash your_db_user&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Accessing psql as the new user we just created. Since we do not have any database associated with our user therefore we are using ‘-d postgres’ to use postgres db for now.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -u&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; your_db_user psql&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; your_db_user&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Create your database.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;CREATE&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; DATABASE your_db_name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is how you can connect with your db if not already connected.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;\c&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; your_db_name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;setting-up-ssl&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#setting-up-ssl&quot; aria-label=&quot;Anchor link for: setting-up-ssl&quot;&gt;🔗&lt;&#x2F;a&gt;Setting Up SSL&lt;&#x2F;h2&gt;
&lt;p&gt;It’s important that we secure our communication with the database while accessing it remotely. We are going to setup SSL certificate using &lt;code&gt;certbot&lt;&#x2F;code&gt; from LetsEncrypt.&lt;&#x2F;p&gt;
&lt;p&gt;Install certbot&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; snap install core&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-terminator&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; snap refresh core&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; apt-get remove certbot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; snap install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --classic&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; certbot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ln&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -s&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;snap&#x2F;bin&#x2F;certbot &#x2F;usr&#x2F;bin&#x2F;certbot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Obtain certificate&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; certbot certonly&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --standalone -d&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; psql.example.com&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Find PostgreSQL data directory&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -u&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres psql&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;SHOW data_directory&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Create a renewal hook file&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nano &#x2F;etc&#x2F;letsencrypt&#x2F;renewal-hooks&#x2F;deploy&#x2F;postgresql.deploy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Paste the following&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;umask 0177&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DOMAIN=psql.example.com&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DATA_DIR=&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;16&#x2F;main&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cp &#x2F;etc&#x2F;letsencrypt&#x2F;live&#x2F;$DOMAIN&#x2F;fullchain.pem $DATA_DIR&#x2F;server.crt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cp &#x2F;etc&#x2F;letsencrypt&#x2F;live&#x2F;$DOMAIN&#x2F;privkey.pem $DATA_DIR&#x2F;server.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;chown postgres:postgres $DATA_DIR&#x2F;server.crt $DATA_DIR&#x2F;server.key&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Save and exit the file.&lt;&#x2F;p&gt;
&lt;p&gt;Give file executable permission&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; chmod +x &#x2F;etc&#x2F;letsencrypt&#x2F;renewal-hooks&#x2F;deploy&#x2F;postgresql.deploy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Get the path of PostgreSQL configuration file&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -u&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres psql&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; postgres&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;SHOW config_file&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Open the file in edit mode and update the SSL section of the file as below:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nano &#x2F;etc&#x2F;postgresql&#x2F;16&#x2F;main&#x2F;postgresql.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ssl = on&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ssl_cert_file = &amp;#39;server.crt&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ssl_key_file = &amp;#39;server.key&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ssl_prefer_server_ciphers = on&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Locate the Connection Settings section and verify the listen_address is to * for all addresses. Make sure the line is not commented out.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;listen_addresses = &amp;#39;*&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Edit &lt;code&gt;pg_hba.conf&lt;&#x2F;code&gt; file which is in the same dir as config file:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nano &#x2F;etc&#x2F;postgresql&#x2F;16&#x2F;main&#x2F;pg_hba.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Add the following line to enable connection over ssl for any user and database:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# TYPE DATABASE USER ADDRESS METHOD&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hostssl all all 0.0.0.0&#x2F;0 scram-sha-256&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hostssl all all ::&#x2F;0 scram-sha-256&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Although in above config it’s shown to allow connection from all IPv4 and IPv6 addresses, however from the security point of view only allow the ip address of your connecting machine (client).&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Renew the SSL certificate&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; certbot renew&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --force-renewal&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Restart PostgreSQL&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; service postgresql restart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Connect to your db remotely&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;psql&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;dbname=your_db_name sslmode=require&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -h&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; psql.example.com&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -U&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; your_db_user&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
    </entry>
</feed>
