pgAdmin 4 Architecture
pgAdmin 4 is the leading Open Source management tool for Postgres. pgAdmin is designed to monitor and manage multiple PostgreSQL and EDB Advanced Server database servers, both local and remote, through a single graphical interface that allows the easy creation and management of database objects, as well as a number of other tools for managing your databases
pgAdmin can be installed in two modes: Desktop & Server mode. Desktop mode is installed as a standalone application being used by the same operating system user while server mode can be accessed over the network, allowing it to be used by multiple users.
Both mode implementations follow a 3 tier architecture approach. This blog explains the pgAdmin architecture and technologies used in both modes.
Server mode can be configured either behind a webserver running as a reverse proxy, or using the WSGI interface. It can be accessed locally or over the network from browsers like Chrome, Firefox, Safari etc. The following architectural diagram shows a simple overview of pgAdmin in server mode. pgAdmin can be run as a WSGI application under Apache HTTPD, using mod_wsgi, standalone using uWSGI or Gunicorn, or under NGINX using uWSGI or Gunicorn. It is also available in a pre-built container that can be run under Docker, Kubernetes, or OpenShift.
The desktop mode is based on NWjs which integrates a chromium browser and NodeJS, and manages pgAdmin's Python server to create a standalone application. When launched the runtime starts the pgAdmin server and opens a window to render the user interface. The below architectural diagram illustrates integration of the pgAdmin Server and NWjs:
The diagram below shows the pgAdmin application architecture:
The pgAdmin server is an instance of a Python application running with the Flask framework. It has the below components.
- SQLite database - This is the backend database used to store required information such as user details, server groups, servers, user preferences etc. pgAdmin uses a python ORM - SQLAlchemy - to define database tables and perform the operations on it. The Flask-Migrate package is used to maintain the database versions. Model definitions can be found at ‘../pgadmin/models/__init__.py’ in the pgAdmin source code.
- pgAdmin Web Application - The Apache HTTPD Server (usually known simply as the web server) is typically used with mod_wsgi to host the user interface & web APIs for pgAdmin, though it is also possible to use WSGI runners such as Gunicorn or uWSGI with other webservers. In Desktop mode, Flask's internal Werkzeug server is used.
The application is implemented using the Flask framework, and communicates with the database servers using the psycopg2 driver. Jinja templates in conjunction with a versioning system to locate the correct version of a template for a particular database server are used to generate dynamic SQL queries to examine and modify databases.
pgAdmin implements a modular code structure. Each module is a folder which contains:
- ‘__init__.py’ where python code is written,
- The templates folder contains .sql files to generate dynamic sql queries to perform required operations on the database server.
- The tests folder contains unit test cases for corresponding modules.
The core of the code structure follows the same Postgres database object hierarchy for e.g. Server Groups > Servers > Databases > Schemas > Tables > Columns.\
Some important additional modules are:
pgAdmin supports multiple third party authentication methods like Kerberos, LDAP and OAuth2. This module contains a method specific python file with code to verify users with the respective authentication mechanisms provided by the third party.
pgAdmin provides different tools which provide user interfaces for command line utilities provided by postgres like backup, restore, maintenance etc. These modules acquire inputs from users though the pgAdmin user interface & executes those operations as background processes.
This module provides utility functions to manage and notify users about long running background processes like backups. It spawns sub processes using operating system level utilities through python & monitors them. The Process logger captures & logs stdout & stderr messages from subprocesses.
This module uses client libraries like boto3 to connect cloud providers and provision postgres instances in the cloud.
The webpack feature, ‘Code Splitting’ is used by pgAdmin to load necessary scripts only. The chunks are split into the four categories: ‘vendor.main, vendor.others, browser_nodes, pgadmin_common’.
pgAdmin application offers administrative and management features for PostgreSQL and EDB Postgres Advanced Server database servers. The pgAdmin architecture is designed in a modular way which makes the pgAdmin easy to understand and flexible for modification and maintenance.