<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Postgres on Micha Kops&#39; Tech Notes</title>
    <link>https://www.hascode.com/tags/postgres/</link>
    <description>Recent content in Postgres on Micha Kops&#39; Tech Notes</description>
    <generator>Hugo -- 0.147.8</generator>
    <language>en</language>
    <copyright>Copyright © 2010 - 2025 Micha Kops. #213243b1d6e8932079e09227d3f3ed0c806cd0c9</copyright>
    <lastBuildDate>Wed, 29 Mar 2023 00:00:00 +0200</lastBuildDate>
    <atom:link href="https://www.hascode.com/tags/postgres/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Postgres with docker-compose or Docker and pg_stat_statements enabled</title>
      <link>https://www.hascode.com/postgres-with-docker-compose-or-docker-and-pg_stat_statements-enabled/</link>
      <pubDate>Wed, 29 Mar 2023 00:00:00 +0200</pubDate>
      <guid>https://www.hascode.com/postgres-with-docker-compose-or-docker-and-pg_stat_statements-enabled/</guid>
      <description>&lt;div id=&#34;preamble&#34;&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://www.postgresql.org/docs/9.3/pgstatstatements.html&#34;&gt;pg_stat_statements&lt;/a&gt; is useful to gather performance information
about queries so lets add it to our dockerized postgres database.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_using_docker_compose&#34;&gt;Using docker-compose&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Using &lt;a href=&#34;https://docs.docker.com/compose/&#34;&gt;docker-compose&lt;/a&gt; we just need to add the following &lt;code&gt;docker-compose.yaml&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;title&#34;&gt;docker-compose.yml&lt;/div&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;version: &amp;#39;3.5&amp;#39;

services:
  postgres:
    container_name: postgres_container
    image: postgres
    ports:
      - &amp;#34;5432:5432&amp;#34;
    volumes:
      - /var/lib/postgresql/data
    # used for query profiling, deactivate for enhanced performance
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: thepassword&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;We simply start our Postgres database with &lt;code&gt;docker-compose&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Quick Apache NiFi Setup with Docker</title>
      <link>https://www.hascode.com/quick-apache-nifi-setup-with-docker/</link>
      <pubDate>Tue, 08 Feb 2022 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/quick-apache-nifi-setup-with-docker/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_steps&#34;&gt;Steps&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Pull image and run with ports exposed:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;docker run --name &amp;#34;nifi&amp;#34; -p 8443:8443 -d apache/nifi:latest&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Fetch the generated username and password from the logs:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;docker logs nifi | grep -A1 &amp;#34;Generated Username&amp;#34;

Generated Username [8f6d91f7-733e-40cf-b900-059ea9dccbf2]
Generated Password [v7KGiiRYLJL2+HzhKOqz1rbgiPOaWz0B]&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Now we may enter the &lt;a href=&#34;https://localhost:8443/nifi/login&#34; class=&#34;bare&#34;&gt;https://localhost:8443/nifi/login&lt;/a&gt; in our browser, accept the security exemption and login
with the credentials from above, voila!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_installing_additional_connectors&#34;&gt;Installing additional connectors&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;I have found a nice summary on the following &lt;a href=&#34;https://github.com/noharm-ai/nifi-docker&#34;&gt;GitHub repository&lt;/a&gt;:&lt;/p&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Setting up multiple Postgresql Instances with docker-compose</title>
      <link>https://www.hascode.com/setting-up-multiple-postgresql-instances-with-docker-compose/</link>
      <pubDate>Tue, 08 Feb 2022 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/setting-up-multiple-postgresql-instances-with-docker-compose/</guid>
      <description>&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;A simple setup when two Postgres databases prefilled with schema/data needed.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;title&#34;&gt;docker-compose.yml&lt;/div&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;version: &amp;#39;3.6&amp;#39;
services:
    postgres1:
        image: postgres
        restart: always
        environment:
            - DATABASE_HOST=127.0.0.1
            - POSTGRES_USER=root
            - POSTGRES_PASSWORD=root
            - POSTGRES_DB=root
        ports:
            - &amp;#34;15432:15432&amp;#34;
        volumes:
            - ./postgres1-init.sql:/docker-entrypoint-initdb.d/docker_postgres_init.sql

    postgres2:
        image: postgres
        restart: always
        environment:
            - DATABASE_HOST=127.0.0.1
            - POSTGRES_USER=root
            - POSTGRES_PASSWORD=root
            - POSTGRES_DB=root

        ports:
            - &amp;#34;25432:25432&amp;#34;
        volumes:
            - ./postgres2-init.sql:/docker-entrypoint-initdb.d/docker_postgres_init.sql&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;And our sample init scripts:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;CREATE USER tester WITH PASSWORD &amp;#39;tester&amp;#39; CREATEDB;
CREATE DATABASE testdb
    WITH
    OWNER = tester
    ENCODING = &amp;#39;UTF8&amp;#39;
    LC_COLLATE = &amp;#39;en_US.utf8&amp;#39;
    LC_CTYPE = &amp;#39;en_US.utf8&amp;#39;
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Using Throwaway Containers for Integration Testing with Java, JUnit 5 and Testcontainers.</title>
      <link>https://www.hascode.com/using-throwaway-containers-for-integration-testing-with-java-junit-5-and-testcontainers./</link>
      <pubDate>Wed, 30 Jan 2019 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/using-throwaway-containers-for-integration-testing-with-java-junit-5-and-testcontainers./</guid>
      <description>&lt;div id=&#34;preamble&#34;&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;A lot of boilerplate code is written when developers need to test their applications with different connected systems like databases, stream platforms and other collaborators.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Docker allows to handle those dependencies but there is still some glue code required to bind the container’s lifecycle and the configuration to the concrete integration test.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Testcontainers is a testing library that offers lightweight throwaway instances of anything able to run in a Docker container, with bindings to configure the specific containers and also provides wrappers to manage our own custom containers.&lt;/p&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Docker Snippets</title>
      <link>https://www.hascode.com/docker-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/docker-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_restrict_network&#34;&gt;Restrict Network&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;admonitionblock tip&#34;&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td class=&#34;icon&#34;&gt;
&lt;i class=&#34;fa icon-tip&#34; title=&#34;Tip&#34;&gt;&lt;/i&gt;
&lt;/td&gt;
&lt;td class=&#34;content&#34;&gt;
Can be useful when using a third-party image that we do not trust
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&#34;sect2&#34;&gt;
&lt;h3 id=&#34;_run_with_no_network&#34;&gt;Run with no network&lt;/h3&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;docker run --network none &amp;lt;image&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect2&#34;&gt;
&lt;h3 id=&#34;_run_with_private_isolated_network&#34;&gt;Run with private isolated network&lt;/h3&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;At least containers attached to this network can talk with another&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;docker network create --internal my_isolated_network
docker run --network my_isolated_network &amp;lt;image&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect2&#34;&gt;
&lt;h3 id=&#34;_block_using_firewall&#34;&gt;Block using firewall&lt;/h3&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;e.g. using &lt;code&gt;iptables&lt;/code&gt; or &lt;code&gt;ipfw&lt;/code&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Get container&amp;#39;s IP
docker inspect -f &amp;#39;{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}&amp;#39; &amp;lt;container_name&amp;gt;

# Block all outbound connections from that IP
sudo iptables -I DOCKER-USER -s &amp;lt;container_ip&amp;gt; -j DROP&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Liquibase Snippets</title>
      <link>https://www.hascode.com/liquibase-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/liquibase-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_precondition_postgres_schema_exists&#34;&gt;Precondition: Postgres Schema exists&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Using this precondition should run a migration only if the schema &lt;code&gt;my_schema&lt;/code&gt; exists.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-xml&#34; data-lang=&#34;xml&#34;&gt;&amp;lt;preConditions onFail=&amp;#34;CONTINUE&amp;#34;&amp;gt;
    &amp;lt;sqlCheck expectedResult=&amp;#34;1&amp;#34;&amp;gt;
        SELECT COUNT(1) FROM information_schema.schemata WHERE schema_name = &amp;#39;my_schema&amp;#39;;
    &amp;lt;/sqlCheck&amp;gt;
&amp;lt;/preConditions&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;ulist&#34;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;https://docs.liquibase.com/concepts/changelogs/preconditions.html&#34; class=&#34;bare&#34;&gt;https://docs.liquibase.com/concepts/changelogs/preconditions.html&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Postgres Snippets</title>
      <link>https://www.hascode.com/postgres-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/postgres-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_get_size_of_a_table&#34;&gt;Get size of a table&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT pg_size_pretty(pg_total_relation_size(&amp;#39;schemaname.tablename&amp;#39;));&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_select_unique_combination_of_fields_tuples&#34;&gt;Select unique combination of fields / tuples&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT DISTINCT ON(field1, field2) field1, field2
FROM thetable&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_select_rows_where_a_combination_of_fields_is_not_unique&#34;&gt;Select rows where a combination of fields is not unique&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT columnA, columnB, count(*) AS count
FROM thetable
GROUP BY columnA, columnB
HAVING count(*) &amp;gt; 1&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_search_for_rows_with_array_containing_value&#34;&gt;Search for rows with array containing value&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Assuming, the field &lt;code&gt;appointments&lt;/code&gt; has the type &lt;code&gt;date[]&lt;/code&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT *
FROM mtable
WHERE appointments @&amp;gt; ARRAY[&amp;#39;2023-09-19&amp;#39;::date]&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>SQL Snippets</title>
      <link>https://www.hascode.com/sql-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/sql-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_count_unique_all_values&#34;&gt;Count unique / all values&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT COUNT(DISTINCT &amp;lt;FIELDNAME&amp;gt;), COUNT(ALL &amp;lt;FIELDNAME&amp;gt;) FROM &amp;lt;TABLE&amp;gt;;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_partially_anonymize_e_mail_addresses&#34;&gt;Partially anonymize e-mail addresses&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;UPDATE &amp;lt;TABLE_NAME&amp;gt;
SET &amp;lt;EMAIL_FIELD&amp;gt;=
INSERT(
&amp;lt;EMAIL_FIELD&amp;gt;,
POSITION(&amp;#39;@&amp;#39; IN &amp;lt;EMAIL_FIELD&amp;gt;),
100,
CONCAT(FLOOR(1 + (RAND() * 100)),&amp;#39;@hascode.com&amp;#39;))
WHERE
POSITION(&amp;#39;@&amp;#39; IN &amp;lt;EMAIL_FIELD&amp;gt;)&amp;gt;0;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_find_duplicate_entries&#34;&gt;Find duplicate entries&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;SELECT COUNT(*), &amp;lt;FIELDNAME&amp;gt; FROM &amp;lt;TABLENAME&amp;gt;
GROUP BY &amp;lt;FIELDNAME&amp;gt;
HAVING COUNT(*)&amp;gt;1;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_mysql_fix_zero_dates_for_data_imports&#34;&gt;MySQL Fix Zero Dates for Data Imports&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;quoteblock&#34;&gt;
&lt;blockquote&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Error: ERROR 1067 (42000) at line 1234: Invalid default value for ‘datefield’&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;In newer MySQL Versions zero in date or zero dates are forbidden .. check this with:&lt;/p&gt;
&lt;/div&gt;</description>
    </item>
  </channel>
</rss>
