<?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: Caleb Mucheru</title>
    <description>The latest articles on Forem by Caleb Mucheru (@cmucheru).</description>
    <link>https://forem.com/cmucheru</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%2F1133342%2F8e1d66c1-08e7-4b37-8629-15a486cd50e0.png</url>
      <title>Forem: Caleb Mucheru</title>
      <link>https://forem.com/cmucheru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cmucheru"/>
    <language>en</language>
    <item>
      <title>Postgres Data Backup with Continuous Archiving and Point-in-time-recovery</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Fri, 02 Feb 2024 12:14:07 +0000</pubDate>
      <link>https://forem.com/cmucheru/postgres-data-backup-with-continuous-archiving-and-point-in-time-recovery-5ech</link>
      <guid>https://forem.com/cmucheru/postgres-data-backup-with-continuous-archiving-and-point-in-time-recovery-5ech</guid>
      <description>&lt;p&gt;This blog is an alternative method to the &lt;a href="https://dev.to/cmucheru/simplified-guide-to-postgresql-data-backup-with-pgdump-and-pgdumpall-3i8j"&gt;SQL dump&lt;/a&gt; method for Postgresql data backup. This method has some advantages over methods for instance it does not need a perfectly consistent file system backup as the starting point.&lt;br&gt;
&lt;strong&gt;Understanding Continuous Archiving&lt;/strong&gt;&lt;br&gt;
Continuous Archiving is a feature in PostgreSQL that enables the continuous streaming of transaction log files (WAL files) to an archive directory. This ensures that a complete history of changes to the database is maintained, allowing for point-in-time recovery.&lt;br&gt;
&lt;strong&gt;Steps to Set Up Continuous Archiving&lt;/strong&gt;&lt;br&gt;
1.Configure PostgreSQL.conf&lt;br&gt;
Modify the PostgreSQL configuration file (postgresql.conf) to enable continuous archiving. Set the wal_level to replica and define the archive_mode and archive_command parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wal_level = replica
archive_mode = on
archive_command = 'cp %p /path/to/archive/%f'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Create Archive Directory&lt;br&gt;
Create the specified archive directory and ensure that the PostgreSQL user has the necessary permissions to write to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir /path/to/archive
chown postgres:postgres /path/to/archive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Restart PostgreSQL&lt;br&gt;
Restart the PostgreSQL service to apply the changes made in the configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Implementing Point-in-time Recovery (PITR)&lt;/strong&gt;&lt;br&gt;
Point-in-time Recovery allows you to restore your database to any specific point in time by replaying the transaction logs. This is invaluable in scenarios where you need to recover from a critical error or data corruption.&lt;/p&gt;

&lt;p&gt;Steps for Point-in-time Recovery&lt;br&gt;
1.Create a Base Backup&lt;br&gt;
Initiate a base backup of the PostgreSQL cluster, which serves as the starting point for the recovery process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_basebackup -D /path/to/backup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Restore the Base Backup&lt;br&gt;
To perform a point-in-time recovery, restore the base backup to a new location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_ctl stop -D /path/to/restore
rm -rf /path/to/restore/*
cp -r /path/to/backup/* /path/to/restore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Apply Transaction Logs&lt;br&gt;
Copy the necessary archived WAL files to the new PostgreSQL cluster and start the server in recovery mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp /path/to/archive/* /path/to/restore/pg_wal
pg_ctl -D /path/to/restore -l /path/to/restore/logfile start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Point-in-time Recovery&lt;br&gt;
Use the pg_wal_replay_resume function to specify the desired recovery target time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql -c "SELECT pg_wal_replay_resume('target_time')"

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

&lt;/div&gt;



&lt;p&gt;Conclusion:&lt;br&gt;
By implementing Continuous Archiving and Point-in-time Recovery in PostgreSQL, you can enhance the resilience of your data management system. Regularly creating base backups and archiving transaction logs ensures that you have a reliable backup strategy in place, minimizing the risk of data loss and allowing for precise recovery to any desired point in time.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
    </item>
    <item>
      <title>Simplified Guide to PostgreSQL Data Backup with pg_dump and pg_dumpall</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Wed, 24 Jan 2024 10:28:07 +0000</pubDate>
      <link>https://forem.com/cmucheru/simplified-guide-to-postgresql-data-backup-with-pgdump-and-pgdumpall-3i8j</link>
      <guid>https://forem.com/cmucheru/simplified-guide-to-postgresql-data-backup-with-pgdump-and-pgdumpall-3i8j</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
In the PostgreSQL ecosystem, ensuring the safety and integrity of your data is paramount. The SQL dump method, facilitated by tools like pg_dump and pg_dumpall, allows you to generate files with SQL commands to recreate databases. Let's explore these tools and their usage for effective data backup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Tools&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;pg_dump&lt;/strong&gt;&lt;br&gt;
Used for backing up individual databases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Basic Usage:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; pg_dump dbname &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dumpfile
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writes the result to standard output, creating a text file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Operates remotely, requiring read access to tables (superuser for entire database).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allows specification of host, port, and user for remote connections.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Advantage: Output generally reloadable into newer PostgreSQL versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internally consistent dumps; does not block other operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;pg_dumpall&lt;/strong&gt;&lt;br&gt;
Backs up entire database clusters, including roles and tablespaces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Basic Usage:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; pg_dumpall &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; dumpfile
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resulting dump can be restored using psql.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Requires database superuser access for full restore.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preserves cluster-wide data, but snapshots of different databases are not synchronized.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Restoring Backups&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Using psql&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  psql dbname &amp;lt; dumpfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Requires manual creation of the database from template0 before execution.&lt;/li&gt;
&lt;li&gt;Users who own objects or were granted permissions must exist before restoring.&lt;/li&gt;
&lt;li&gt;Option to run psql with &lt;code&gt;--set ON_ERROR_STOP=on&lt;/code&gt; for better error handling.&lt;/li&gt;
&lt;li&gt;Specify &lt;code&gt;--single-transaction&lt;/code&gt; for a fully completed or fully rolled-back restore.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Handling Large Databases&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Compressed Dumps&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pg_dump dbname | &lt;span class="nb"&gt;gzip&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; filename.gz
  &lt;span class="nb"&gt;gunzip&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; filename.gz | psql dbname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Split Output Files&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pg_dump dbname | &lt;span class="nb"&gt;split&lt;/span&gt; &lt;span class="nt"&gt;-b&lt;/span&gt; 2G - filename
  &lt;span class="nb"&gt;cat &lt;/span&gt;filename&lt;span class="k"&gt;*&lt;/span&gt; | psql dbname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Custom Dump Format&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pg_dump &lt;span class="nt"&gt;-Fc&lt;/span&gt; dbname &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; filename
  pg_restore &lt;span class="nt"&gt;-d&lt;/span&gt; dbname filename
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parallel Dump&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pg_dump &lt;span class="nt"&gt;-j&lt;/span&gt; num &lt;span class="nt"&gt;-F&lt;/span&gt; d &lt;span class="nt"&gt;-f&lt;/span&gt; out.dir dbname
  pg_restore &lt;span class="nt"&gt;-j&lt;/span&gt; to restore dump &lt;span class="k"&gt;in &lt;/span&gt;parallel.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Parallel dumps for the "directory" archive format.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>An introduction to Postgres 🐘 Prepared Statements</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Mon, 15 Jan 2024 14:16:00 +0000</pubDate>
      <link>https://forem.com/cmucheru/an-introduction-to-postgres-prepared-statements-1nd0</link>
      <guid>https://forem.com/cmucheru/an-introduction-to-postgres-prepared-statements-1nd0</guid>
      <description>&lt;p&gt;In the realm of database management, optimizing query performance is a crucial aspect of ensuring the efficiency of applications. Postgres, a robust open-source relational database, offers a powerful feature known as "Prepared Statements" to enhance the execution of SQL queries. In this blog post, we'll delve into the concept of Postgres Prepared Statements, exploring their application in tables and composite types, providing sample SQL code, and elucidating the syntax for creating these statements. Additionally, we'll highlight the merits of leveraging Prepared Statements in your database interactions.&lt;br&gt;
The syntax for creating Prepared Statements in Postgres involves two primary commands: &lt;code&gt;PREPARE&lt;/code&gt; and &lt;code&gt;EXECUTE&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PREPARE statement_name [ (data_type [, ...]) ] AS
    SQL_query_string;

EXECUTE statement_name [ (parameter [, ...]) ];

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

&lt;/div&gt;



&lt;p&gt;Let us explore the concept with both tables and composite types.&lt;br&gt;
(a) In Tables&lt;br&gt;
Prepared Statements in Postgres allow for the precompilation and reuse of SQL queries. In the context of tables, this translates to improved performance by reducing the overhead of parsing and planning queries for repetitive executions. Prepared Statements can be especially beneficial when dealing with frequently executed queries involving tables.&lt;/p&gt;

&lt;p&gt;Example 1: Prepared Statement for Selecting Data from a Table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PREPARE select_employee (INT) AS
    SELECT * FROM employees WHERE employee_id = $1;

EXECUTE select_employee(1001);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(b) In Composite Types&lt;br&gt;
Beyond tables, Prepared Statements extend their utility to composite types. Composite types in Postgres enable the grouping of related fields into a single user-defined type. Leveraging Prepared Statements in conjunction with composite types enhances code modularity and readability, making it easier to manage complex data structures.&lt;/p&gt;

&lt;p&gt;Example 1: Prepared Statement for Selecting Data from a Table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TYPE person_type AS (
    name VARCHAR,
    age INT
);

PREPARE insert_person (person_type) AS
    INSERT INTO people VALUES ($1);

EXECUTE insert_person(ROW('John Doe', 30));

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

&lt;/div&gt;



&lt;p&gt;Why Use Prepared Statements?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Performance Optimization: Prepared Statements reduce query processing overhead by preparing and planning queries only once, resulting in faster execution for repeated queries.&lt;/li&gt;
&lt;li&gt;Parameterized Queries: Prepared Statements support parameterized queries, allowing for dynamic data input. This not only enhances security by preventing SQL injection but also promotes code reusability.&lt;/li&gt;
&lt;li&gt;Execution Plan Reuse: Postgres caches the execution plan of a Prepared Statement, allowing the database engine to reuse the plan for subsequent executions, further contributing to performance gains.&lt;/li&gt;
&lt;li&gt;Reduced Network Traffic: Parameter values are sent separately from the query, minimizing network traffic, which is especially beneficial in scenarios with frequent query executions.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>postgres</category>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>Unlocking the Power of PostgreSQL Extensions: A Developer's Toolkit</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Tue, 09 Jan 2024 17:05:09 +0000</pubDate>
      <link>https://forem.com/cmucheru/unlocking-the-power-of-postgresql-extensions-a-developers-toolkit-40mf</link>
      <guid>https://forem.com/cmucheru/unlocking-the-power-of-postgresql-extensions-a-developers-toolkit-40mf</guid>
      <description>&lt;p&gt;While PostgreSQL's core features are robust, its true versatility lies in the extensive ecosystem of extensions. These extensions act as specialized tools, empowering developers to tailor their database experience to specific needs and enhance their workflow. Let's explore some of the most valuable extensions you can leverage:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Apache-AGE&lt;br&gt;
Apache AGE provides graph database functionality. AGE is an acronym for A Graph Extension.To learn more on Apache-AGE from the set-up to using AGE for your database visit the Apache-AGE documentation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PostGIS&lt;br&gt;
PostGIS is an open source software program that adds support for geographic objects to the PostgreSQL object-relational database. PostGIS follows the Simple Features for SQL specification from the Open Geospatial Consortium.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pg_stat_statements&lt;br&gt;
pg_stat_statements provides a way to track execution statistics of SQL statements within a PostgreSQL database. This extension is particularly useful for database administrators and developers to analyze the performance of queries and identify potential optimization opportunities.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;4.pgcrypto &lt;br&gt;
pgcrypto enables you to encrypt, decrypt, hash, and create digital signatures within your database&lt;/p&gt;

&lt;p&gt;Protecting sensitive data is paramount, and pgcrypto is your shield. This extension encrypts data at rest and in transit, safeguarding confidential details from prying eyes. Store hashed passwords securely, encrypt sensitive information like credit card numbers, and perform cryptographic operations – all within the secure enclave of your PostgreSQL database. &lt;/p&gt;

&lt;p&gt;5.citext&lt;br&gt;
citext module provides a case-insensitive character string type, citext. Essentially, it internally calls lower when comparing values.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://age.apache.org/age-manual/"&gt;Apache-AGE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://postgis.net/documentation/getting_started/"&gt;PostGIS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/pgstatstatements.html"&gt;pgstatements&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/pgcrypto.html"&gt;pgcrypto&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/citext.html"&gt;citext&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>Working with multiple versions of PostgreSQL</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Mon, 23 Oct 2023 15:30:03 +0000</pubDate>
      <link>https://forem.com/cmucheru/working-with-multiple-versions-of-postgresql-4o5b</link>
      <guid>https://forem.com/cmucheru/working-with-multiple-versions-of-postgresql-4o5b</guid>
      <description>&lt;p&gt;Running different versions of PostgreSQL locally can be useful for both production and development purposes.There are several ways you could achieve this; by using Docker,by specifying the absolute path for the specific version you want to run,and by using &lt;code&gt;pg_wrapper&lt;/code&gt; to enable easier switching between different versions of PostgreSQL.&lt;br&gt;
We are going to see how we can use the three methods to achieve this:&lt;br&gt;
&lt;strong&gt;Using absolute paths in your terminal&lt;/strong&gt;&lt;br&gt;
First, we need to have the different versions of PostgreSQL installed. When you run a command like,&lt;br&gt;
&lt;code&gt;pg_config --version&lt;/code&gt;, the PostgreSQL version that pops up is the first where the PATH is set first. To run the second version of PostgreSQL,we need to specify the absolute path of its installation. There is an easy way to determine where the installation of either of the PostgreSQL versions is, by finding it in the terminal.&lt;br&gt;
In the terminal, we run&lt;br&gt;
&lt;code&gt;su -&lt;/code&gt; to change user to root, then we find it by&lt;br&gt;
&lt;code&gt;sudo find / -name pg_config&lt;/code&gt;.&lt;br&gt;
The command return the &lt;code&gt;pg_config&lt;/code&gt; absolute paths in which there is a bin directory, for instance, &lt;code&gt;/usr/local/pgsql/bin/pg_config/pg_config&lt;/code&gt;. &lt;br&gt;
When we run the absolute path above with the &lt;code&gt;--version&lt;/code&gt; parameter&lt;br&gt;
like this &lt;code&gt;/usr/local/pgsql/bin/pg_config/pg_config --version&lt;/code&gt;&lt;br&gt;
, we get the version of the PostgreSQL in that path.&lt;br&gt;
We can then set the path for our PostgreSQL version in this way: &lt;code&gt;export PATH=$PATH:/usr/local/pgsql/bin&lt;/code&gt; to make &lt;code&gt;initdb postgres&lt;/code&gt; to create &lt;code&gt;postgres&lt;/code&gt; cluster and later &lt;code&gt;createdb sampledb&lt;/code&gt; to create &lt;code&gt;sampledb&lt;/code&gt; in the &lt;code&gt;postgres&lt;/code&gt; cluster. &lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>Spinning up Apache-AGE on Docker</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Wed, 27 Sep 2023 11:14:38 +0000</pubDate>
      <link>https://forem.com/cmucheru/spinning-up-apache-age-on-docker-3bda</link>
      <guid>https://forem.com/cmucheru/spinning-up-apache-age-on-docker-3bda</guid>
      <description>&lt;p&gt;This is post recaps on previous posts on using AGE in Docker to entering the &lt;code&gt;psql&lt;/code&gt; terminal to using AGE there. &lt;br&gt;
&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You need Postgres to be installed. If you do not have it, you can get it from &lt;a href="https://github.com/postgres/postgres"&gt;here&lt;/a&gt; and build from the source.&lt;/li&gt;
&lt;li&gt;Have Docker installed. &lt;a href="https://www.docker.com/get-started/"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In your PostgreSQL installation's bin folder set up the Postgres server and create you database&lt;br&gt;
&lt;code&gt;initdb postgres&lt;/code&gt;&lt;br&gt;
This will give you some command(s) to run.&lt;br&gt;
Create your database:&lt;br&gt;
&lt;code&gt;createdb postgres&lt;/code&gt;&lt;br&gt;
Get the apache-age image from the Docker Hub&lt;br&gt;
&lt;code&gt;sudo docker pull apache/age&lt;/code&gt;&lt;br&gt;
Run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run \
    --name myPostgresDb  \
    -p 5455:5432 \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_PASSWORD=your_password \
    -e POSTGRES_DB=postgresDB \
    -d \
    apache/age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Frome the command above:&lt;br&gt;
--name Assign a name to the container&lt;br&gt;
-p Publish a container’s port(s) to the host&lt;br&gt;
-e Set environment variables&lt;br&gt;
-d Run container in background and print container ID&lt;/p&gt;

&lt;p&gt;Run the following command to get into psql terminal&lt;br&gt;
&lt;code&gt;psql -h localhost -p 5455 -U postgres&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Then do the following to create and load the extension:
postgres=# CREATE EXTENSION age;
CREATE EXTENSION
postgres=# LOAD 'age';
LOAD
postgres=# SET search_path=ag_catalog, "$user", public;
SET
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://age.apache.org/age-manual/master/intro/setup.html"&gt;Age Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hub.docker.com/r/apache/age"&gt;Docker Hub Apache AGE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>age</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Setting up a Simple PostgreSQL Database</title>
      <dc:creator>Caleb Mucheru</dc:creator>
      <pubDate>Sun, 03 Sep 2023 07:52:29 +0000</pubDate>
      <link>https://forem.com/cmucheru/setting-up-a-simple-postgresql-database-28hk</link>
      <guid>https://forem.com/cmucheru/setting-up-a-simple-postgresql-database-28hk</guid>
      <description>&lt;p&gt;PostgreSQL is a robust open-source relational database management system. Setting up a simple PostgreSQL database on your  system allows you to interact with the system before before integrating extensions. We will cover some steps, starting with the download and installation of PostgreSQL version 12.15. We are going to do the installation from the source.&lt;br&gt;
Step 1: Download PostgreSQL&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a terminal in your system.&lt;/li&gt;
&lt;li&gt;Clone the repository to your system:
We are going to use the unofficial github repository of  Postgres from &lt;a href="https://github.com/postgres/postgres"&gt;&lt;/a&gt;
In your terminal run &lt;code&gt;git clone -b REL_12_STABLE https://github.com/postgres/postgres.git&lt;/code&gt;
This will clone the specified version of PostgreSQL, version 12 from the repo.&lt;/li&gt;
&lt;li&gt;Update your package list:
&lt;code&gt;sudo apt-get update&lt;/code&gt;
Step 2: Install PostgreSQL
Navigate to the the postgres installation folder 
Then run the following command to configure your files:
&lt;code&gt;./configure&lt;/code&gt;
Run this command to install PostgreSQL
&lt;code&gt;sudo make install&lt;/code&gt;
Run the following command to make sure that PostgreSQL will work as expected by developers.
make check
Step 3: Start the PostgreSQL Service
After the installation, PostgreSQL should automatically start. However, you can ensure it's running with this command:
&lt;code&gt;sudo systemctl start postgresql&lt;/code&gt;
This command starts the PostgreSQL service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 4: Create a Database User&lt;br&gt;
By default, PostgreSQL uses the system username to authenticate. To create a PostgreSQL user with your system username and grant necessary privileges, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Access the PostgreSQL interactive terminal:
&lt;code&gt;sudo -u postgres psql&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the PostgreSQL terminal, create a user (replace &lt;code&gt;your_username&lt;/code&gt; with your actual system username):
&lt;code&gt;CREATE USER your_username;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set a password for the user (replace &lt;code&gt;your_password&lt;/code&gt; with your desired password):
&lt;code&gt;ALTER USER your_username PASSWORD 'your_password';&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Grant superuser privileges to the user:
&lt;code&gt;ALTER USER your_username WITH SUPERUSER;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Exit the PostgreSQL terminal by &lt;code&gt;\q&lt;/code&gt;
Step 5: Create a Database
Now,  create a database with your user's privileges. In the terminal, run:
&lt;code&gt;createdb testdb&lt;/code&gt;
This command creates a database named "testdb."
Step 6: Connect to the Database&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Connect to your newly created database using the &lt;code&gt;psql&lt;/code&gt; command. Replace &lt;code&gt;your_username&lt;/code&gt; with your system username and &lt;code&gt;testdb&lt;/code&gt; with the database name if necessary:&lt;br&gt;
&lt;code&gt;psql -U your_username -d testdb&lt;/code&gt;&lt;br&gt;
Step 7: Create a Table and Insert Data&lt;br&gt;
Once connected to the database, let’s create a table and insert data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE employees (
    id serial PRIMARY KEY,
    first_name varchar(50),
    last_name varchar(50),
    salary numeric
);
&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;INSERT INTO employees (first_name, last_name, salary)
VALUES ('John', 'Doe', 50000),
       ('Jane', 'Smith', 60000),
       ('Alice', 'Johnson', 55000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 8: Query the Database&lt;/p&gt;

&lt;p&gt;Let’s query the database to retrieve information from the "employees" table&lt;br&gt;
&lt;code&gt;SELECT * FROM employees;&lt;/code&gt;&lt;br&gt;
The results of the above query is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; id | first_name | last_name | salary 
----+------------+-----------+--------
  1 | John       | Doe       |  50000
  2 | Jane       | Smith     |  60000
  3 | Alice      | Johnson   |  55000
(3 rows)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! You've successfully set up a PostgreSQL database, starting from downloading and installing PostgreSQL.&lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
  </channel>
</rss>
