Package: pgmob
PGMob - PostgreSQL Management ObjectsPGMob is a Python package that helps to simplify PostgreSQL administration by providing a layer of abstraction that allows you
to write simple and easily understandable code instead of having to rely on SQL syntax. It's your one tool that helps you to
manage your PostgreSQL clusters on any scale and automate routine operations with ease.PGMob abstracts away the complexity of SQL code and presents a user with a easy to use interface that controls most of
the aspects of PostgreSQL administration. It will ensure you won't have to switch between Python and SQL while building
automation tasks and it will guide you through the process with type helpers and examples.With PGMob, you can:Control your server while having access to only PostgreSQL protocolEnsure users, databases, and database objects define as you want themExecute backup/restore operations on your server without having to remember the command syntaxScript and export your database objects on the flyInstallingPGMob requires an adapter to talk to PostgreSQL, which it can detect automatically. Currently supported adapters:psycopg2To install the module without an adapter (you would have to download it by other means) use$pipinstall-UpgmobTo include the adapter, use pip extras feature:$pipinstall-Upgmob[psycopg2]DocumentationTBDExample codefrompgmobimportClustercluster=Cluster(host="127.0.0.1",user="postgres",password="s3cur3p@ss")# Execute a simple query with parameterscluster.execute("SELECT tableowner FROM pg_tables WHERE tablename LIKE%s","pg*")# Create a new database owner and reassign ownershipowner_role=cluster.roles.new(name="db1owner",password="foobar")owner_role.create()db=cluster.databases["db1"]db.owner=owner_role.namedb.alter()# Modify pg_hba on the fly:entry="host all all 127.0.0.1/32 trust"ifentrynotincluster.hba_rules:cluster.hba_rules.extend(entry)cluster.hba_rules.alter()# clone an existing rolesql=cluster.roles["somerole"].script()cluster.execute(sql.replace("somerole","newrole"))# control access to your databasecluster.terminate(databases=["somedb"],roles=["someapp"])cluster.databases["someotherdb"].disable()# run backups/restoresfrompgmob.backupimportFileBackup,FileRestorefile_backup=FileBackup(cluster=cluster)file_backup.options.schema_only=Truefile_backup.backup(database="db1",path="/tmp/db.bak")cluster.databases.new("db2").create()file_restore=FileRestore(cluster=cluster)file_restore.restore(database="db2",path="/tmp/db.bak")# create, modify, and drop objectscluster.schemas.new("app_schema").create()fortin[tfortincluster.tablesift.schema=="old_schema"]:t.schema="app_schema"t.alter()cluster.schemas["old_schema"].drop()Dynamic objects and collectionsEach Python object in PGMob is asynchronously connected to the corresponding object on the server. When changing object attributes,
one only changes the local object. In order to push the changes to the server, one needs to execute the.alter()method of the dynamic
object solidyfing the changes on the server.When working with collections, such as tables, procedures, and others, you can retrieve corresponding objects using their name as index:cluster.tables["tablename"]# or, in case the schema is not publiccluster.tables["myschema.tablename"]However, you can iterate upon such collections as if they were a list:fortincluster.tables:t.owner="new_owner"t.alter()if"myschema.tab1"incluster.tables:cluster.tables["myschema.tab1"].drop()This helps the developer to write a concise and readable code when working with PostgreSQL objects.LinksTBD