Metadata-Version: 2.2
Name: bisos.postgres
Version: 0.11
Summary: bisos.postgres: PostgreSQL as the bedrock database of BISOS
Author: Mohsen Banan
Author-email: libre@mohsen.1.banan.byname.net
Maintainer: Mohsen Banan
Maintainer-email: libre@mohsen.1.banan.byname.net
License: AGPL
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: bisos
Requires-Dist: bisos.b
Requires-Dist: bisos.capability
Requires-Dist: bisos.common
Requires-Dist: bisos.csPlayer
Requires-Dist: bisos.sbom
Requires-Dist: setuptools==75.8.0
Requires-Dist: wheel==0.38.4
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: license
Dynamic: maintainer
Dynamic: maintainer-email
Dynamic: requires-dist
Dynamic: summary

===========================================================
bisos.postgres: PostgreSQL as the bedrock database of BISOS
===========================================================

.. contents::
   :depth: 3
..

Overview
========

*bisos.postgres* makes PostgreSQL the **bedrock** of BISOS: the sole
database, and a generic layer on which other capabilities are built.
Airflow, django-based packages and every other capability that needs a
database converge on it, each as a **tenant**. In the BISOS model many
capabilities co-exist consistently on one host (typically one of several
multi-tenant VMs), and a single, well-known database layer is what lets
them do so.

The package is a BISOS Capability Bundle (CBS/CBM). A planted pointer
leaf (``cbmProc.spcs`` at ``sys/cbm/collective/bedrock/postgres/``)
subprocess-invokes a Capability Bundle Specification
(``postgres-cbs.pcs``), whose bundle is the package installation
(``postgres-sbom.pcs``). The CBM walker processes the ``bedrock``
capabilityType before all other capability types, so tenants find the
cluster already running.

Package Documentation At Github
===============================

The information below is a subset of the full of documentation for this
bisos-pip package. More complete documentation is available at:
https://github.com/bisos-pip/postgres-cs

.. _table-of-contents:

Table of Contents TOC
=====================

-  `Overview <#overview>`__
-  `Package Documentation At
   Github <#package-documentation-at-github>`__
-  `The Tenant Model <#the-tenant-model>`__
-  `What This Package Does And Does Not
   Do <#what-this-package-does-and-does-not-do>`__
-  `Installation <#installation>`__

   -  `Installation With pip <#installation-with-pip>`__
   -  `Installation With pipx <#installation-with-pipx>`__

-  `Usage <#usage>`__

   -  `Cluster and Tenants Information:
      ``postgresAdmin.cs`` <#cluster-and-tenants-information-postgresadmincs>`__
   -  `Direct PostgreSQL Cheat Sheet:
      ``quickPostgres.cs`` <#direct-postgresql-cheat-sheet-quickpostgrescs>`__

-  `Clients <#clients>`__

   -  `VS Code <#vs-code>`__

-  `Key Files <#key-files>`__
-  `Part of BISOS — ByStar Internet Services Operating
   System <#part-of-bisos--bystar-internet-services-operating-system>`__
-  `Documentation and Blee-Panels <#documentation-and-blee-panels>`__
-  `Support <#support>`__
-  `Planned Improvements <#planned-improvements>`__

   -  `Decide the interface through which a consumer asks for its role
      and
      database <#decide-the-interface-through-which-a-consumer-asks-for-its-role-and-database>`__
   -  `Adopt the live cluster of this host, with its existing airflow
      tenant, without re-creating
      anything <#adopt-the-live-cluster-of-this-host-with-its-existing-airflow-tenant-without-re-creating-anything>`__
   -  `Own pg\ hba.conf, backup and restore, and major-version upgrades
      at the bedrock
      level <#own-pg_hbaconf-backup-and-restore-and-major-version-upgrades-at-the-bedrock-level>`__
   -  `Confirm whether Microsoft's VS Code PostgreSQL extension can use
      the unix socket with peer authentication; if not, decide how
      bedrock grants a TCP and password
      exception <#confirm-whether-microsofts-vs-code-postgresql-extension-can-use-the-unix-socket-with-peer-authentication-if-not-decide-how-bedrock-grants-a-tcp-and-password-exception>`__
   -  `Confirm DBeaver Community Edition's telemetry setting and its
      unix-socket connection on a current
      version <#confirm-dbeaver-community-editions-telemetry-setting-and-its-unix-socket-connection-on-a-current-version>`__

The Tenant Model
================

A **tenant** of the bedrock database is a PostgreSQL role plus a
database of the same name, owned by that role. The tenant's service runs
as an OS account of the same name, and connects over the unix socket
(``/var/run/postgresql``) with **peer authentication**: PostgreSQL maps
the connecting OS account to the like-named role. No password and no
secret exists anywhere.

The administrator is the ``postgres`` OS account (and role) that Debian
creates with the ``postgresql`` package. Only the bedrock capability
administers the cluster; a tenant never does.

A tenant unit declares its dependency with ``Requires=/=After``
``postgresql.service``.

What This Package Does And Does Not Do
======================================

Does:

#. Installs the ``postgresql`` server and the ``libpq-dev`` client
   headers (apt).
#. Makes sure the cluster is enabled and running, discovering its
   version and name rather than hardcoding them.
#. Sets ``max_connections`` to at least 200, the baseline every tenant
   may assume. The value is server-wide, so it is owned here and not by
   any one consumer.

Does not:

-  Author a systemd unit. Debian's ``postgresql.service`` is used as is.
-  Provision tenants (roles and databases). For now each consumer
   creates its own; a shared provisioning interface is an open item (see
   Planned Improvements).
-  Install Python drivers (``psycopg2``, ``asyncpg``, …). Those belong
   to each consumer's sbom.

Installation
============

| The sources for the bisos.postgres pip package are maintained at:
| https://github.com/bisos-pip/postgres

| The bisos.postgres pip package is available at PYPI as
| https://pypi.org/project/bisos.postgres

You can install bisos.postgres with pip or pipx.

Installation With pip
---------------------

If you need access to bisos.postgres as a python module, install it with
pip:

.. code:: bash

   pip install bisos.postgres

Installation With pipx
----------------------

If you only need access to bisos.postgres on the command line, install
it with pipx:

.. code:: bash

   pipx install bisos.postgres

The following commands are made available:

-  ``postgresAdmin.cs`` — read-only information about this host's
   cluster and its tenants.
-  ``quickPostgres.cs`` — direct-command cheat sheet for ``psql``,
   ``pg_*`` and the ``postgresql`` service.
-  ``postgres-cbs.pcs`` — Capability Bundle Specification: declares the
   sbom.
-  ``cbmProc-postgres.spcs`` — planted CBM pointer that materializes the
   CBS at this host.
-  ``postgres-sbom.pcs`` — package installation via
   `bisos.sbom <https://github.com/bisos-pip/sbom>`__.
-  ``dbClients-sbom.pcs`` — the database clients, run by
   ``postgres-sbom.pcs``; and ``dbeaver-sbom.pcs``, the DBeaver
   Community Edition client, run by it.

Usage
=====

Cluster and Tenants Information: ``postgresAdmin.cs``
-----------------------------------------------------

Run with no arguments (or ``-i examples``) to see the full menu. Key
sub-commands:

.. code:: bash

   postgresAdmin.cs -i clusterInfo   # cluster, service, version, data dir, max_connections
   postgresAdmin.cs -i tenantsInfo   # roles, databases, connections
   postgresAdmin.cs -i fullUpdate    # both together

Direct PostgreSQL Cheat Sheet: ``quickPostgres.cs``
---------------------------------------------------

For direct ``psql`` / ``pg_lsclusters`` / ``systemctl`` / ``journalctl``
invocations, run:

.. code:: bash

   quickPostgres.cs

Clients
=======

Standardizing on PostgreSQL also means choosing which clients are used
with it. There is no single tool; there is one per context, layered:

+----------------------+----------------------+----------------------+
| Context              | Client               | Notes                |
+======================+======================+======================+
| Every host, every VM | ``psql``             | The baseline. Works  |
|                      | (``                  | with peer            |
|                      | postgresql-client``) | authentication over  |
|                      |                      | the unix socket, no  |
|                      |                      | password.            |
+----------------------+----------------------+----------------------+
| Blee (Libre-Halaal,  | ``sql-mode`` and     | SQL lives in org     |
| Emacs)               | org-babel SQL;       | files beside the     |
|                      | `PGmacs <htt         | documentation.       |
|                      | ps://emacsconf.org/2 | PGmacs browses and   |
|                      | 024/talks/pgmacs>`__ | edits tables over    |
|                      |                      | the network or a     |
|                      |                      | unix socket.         |
+----------------------+----------------------+----------------------+
| Graphical, optional  | `DBeaver <https://d  | Apache 2.0. Over a   |
|                      | beaver.io/about/>`__ | unix socket it needs |
|                      | Community Edition    | junixsocket driver   |
|                      |                      | properties.          |
+----------------------+----------------------+----------------------+
| VS Code, in          | Microsoft's          | See below.           |
| proprietary          | `PostgreSQL          |                      |
| environments         | ext                  |                      |
|                      | ension <https://lear |                      |
|                      | n.microsoft.com/en-u |                      |
|                      | s/azure/postgresql/e |                      |
|                      | xtensions/vs-code-ex |                      |
|                      | tension/overview>`__ |                      |
+----------------------+----------------------+----------------------+

The clients are installed by a group sbom, ``dbClients-sbom.pcs``:
``postgresql-client`` (``psql``) and, through its own file
``dbeaver-sbom.pcs``, DBeaver. The group stands alone, for a platform
that needs only clients to reach a cluster on another host;
``postgres-sbom.pcs`` pulls it in, after the server steps, so a host
that runs the server gets the clients too and a failure in a client
cannot undo the server. DBeaver is not in Debian's own apt, so it comes
from the vendor repository; the vendor key is fetched over https and
trusted on first use. DBeaver Community Edition sends anonymous usage
statistics by default; each user can switch that off under Window >
Preferences > General > Usage Statistics. The BISOS Platform is
edge-oriented and includes a desktop, so a graphical client on a host is
normal.

VS Code
-------

Some environments require VS Code, and they are proprietary ones. For
them the client is Microsoft's PostgreSQL extension for VS Code
(``ms-ossdata.vscode-postgresql`` in the Marketplace). Blee remains the
Libre-Halaal answer; VS Code is an accommodation, not a replacement for
it.

The extension's documented connection profile asks for a hostname,
database, user and password. **Whether it can connect over the unix
socket with peer authentication has not been confirmed.** If it cannot,
using it against a bedrock cluster needs a TCP listener, a
``pg_hba.conf`` entry and a password: an exception to the tenant model,
where no secret exists anywhere. Such an exception is for the bedrock
layer to own and to grant deliberately, not for a tenant or a developer
to configure ad hoc. See Planned Improvements.

Key Files
=========

An overview of the relevant files of the bisos.postgres package
(starting point; to be expanded as the package develops):

-  ``py3/bin/postgres-sbom.pcs`` — package installation: server, client
   headers, cluster running with the connection baseline.
-  ``py3/bin/dbClients-sbom.pcs`` — the database clients:
   ``postgresql-client``, and ``dbeaver-sbom.pcs``; stands alone, and is
   pulled in by ``postgres-sbom.pcs``.
-  ``py3/bin/dbeaver-sbom.pcs`` — DBeaver Community Edition: the vendor
   apt source and the package; pulled in by ``dbClients-sbom.pcs``.
-  ``py3/bin/postgres-cbs.pcs`` — CBS declaring the bundle (the sbom
   alone: no assemble step, no units).
-  ``py3/bin/cbmProc-postgres.spcs`` — planted CBM pointer leaf for
   ``sys/cbm/collective/bedrock/postgres/``.
-  ``py3/bin/postgresAdmin.cs`` — cluster and tenants information
   (clusterInfo, tenantsInfo, fullUpdate).
-  ``py3/bin/quickPostgres.cs`` — direct-command cheat sheet.
-  ``py3/bisos/postgres/`` — the library surface; empty so far.
-  ``py3/tests/verify.sh`` — smoke test for the command services.
-  ``py3/setup.py``, ``py3/pypiProc.sh`` — PyPI packaging (setup.py is
   dblock-driven; do not hand-edit).

Part of BISOS — ByStar Internet Services Operating System
=========================================================

Layered on top of Debian, **BISOS** (By\* Internet Services Operating
System) is a unified and universal framework for developing both
internet services and software-service continuums that use internet
services. See `Bootstrapping ByStar, BISOS and
Blee <https://github.com/bxGenesis/start>`__ for information about
getting started with BISOS.

**BISOS** is a foundation for **The Libre-Halaal ByStar Digital
Ecosystem** which is described as a cure for losses of autonomy and
privacy in a book titled: `Nature of
Polyexistentials <https://github.com/bxplpc/120033>`__

*bisos.postgres* is part of BISOS. It is a standalone package that can
be used independently of the full BISOS environment.

Documentation and Blee-Panels
=============================

bisos.postgres is part of the ByStar Digital Ecosystem
http://www.by-star.net.

This module's primary documentation is in the form of Blee-Panels.
Blee-Panels are in the ``./panels`` directory. From within Blee and
BISOS these panels are accessible under the Blee "Panels" menu.

See
`file:./panels/bisos.postgres/_nodeBase_/fullUsagePanel-en.org <./panels/bisos.postgres/_nodeBase_/fullUsagePanel-en.org>`__
for a starting point.

*bisos.postgres* is best developed with
`Blee <https://github.com/bx-blee>`__, the *By\* BISOS Libre-Halaal
Emacs Environment* — a layer on top of Emacs and BISOS which creates a
comprehensive integrated usage and development environment.

Support
=======

| For support, criticism, comments and questions; please contact the
  author/maintainer
| `Mohsen Banan <http://mohsen.1.banan.byname.net>`__ at:
  http://mohsen.1.banan.byname.net/contact

Planned Improvements
====================

.. _decide-the-interface-through-which-a-consumer-asks-for-its-role-and-database:

TODO Decide the interface through which a consumer asks for its role and database
---------------------------------------------------------------------------------

.. _adopt-the-live-cluster-of-this-host-with-its-existing-airflow-tenant-without-re-creating-anything:

TODO Adopt the live cluster of this host, with its existing airflow tenant, without re-creating anything
--------------------------------------------------------------------------------------------------------

.. _own-pghba.conf-backup-and-restore-and-major-version-upgrades-at-the-bedrock-level:

TODO Own pg\ :sub:`hba`.conf, backup and restore, and major-version upgrades at the bedrock level
-------------------------------------------------------------------------------------------------

.. _confirm-whether-microsofts-vs-code-postgresql-extension-can-use-the-unix-socket-with-peer-authentication-if-not-decide-how-bedrock-grants-a-tcp-and-password-exception:

TODO Confirm whether Microsoft's VS Code PostgreSQL extension can use the unix socket with peer authentication; if not, decide how bedrock grants a TCP and password exception
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

.. _confirm-dbeaver-community-editions-telemetry-setting-and-its-unix-socket-connection-on-a-current-version:

TODO Confirm DBeaver Community Edition's telemetry setting and its unix-socket connection on a current version
--------------------------------------------------------------------------------------------------------------
