{"id":21171600,"url":"https://github.com/0k/pgm","last_synced_at":"2025-07-09T19:33:01.197Z","repository":{"id":25194597,"uuid":"28618208","full_name":"0k/pgm","owner":"0k","description":"Postgres DB manager - list, copy, delete, chown postgres databases in one command.","archived":false,"fork":false,"pushed_at":"2022-02-01T17:09:04.000Z","size":52,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T06:11:53.458Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"kevin28520/My-TensorFlow-tutorials","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0k.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-30T04:58:22.000Z","updated_at":"2021-12-08T11:21:37.000Z","dependencies_parsed_at":"2022-08-23T18:01:05.130Z","dependency_job_id":null,"html_url":"https://github.com/0k/pgm","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/0k/pgm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0k%2Fpgm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0k%2Fpgm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0k%2Fpgm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0k%2Fpgm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0k","download_url":"https://codeload.github.com/0k/pgm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0k%2Fpgm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264502490,"owners_count":23618617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T16:08:36.532Z","updated_at":"2025-07-09T19:33:00.938Z","avatar_url":"https://github.com/0k.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"Postgres manager\n================\n\n``pgm`` is a simple tool to manage postgres databases: copy, delete,\nlisting, saving local and distant database in the most simple way\nthrough the command line.\n\nIt leverage ``ssh`` and ``psql`` to chain commands that you would\nprobably use yourself to achieve the similar task. Cutting open\nconnection, using quick copy when available, making backups and/or\ndeleting target database to allow easy overwriting, changing ownership\nor verifying that your postgres version are compatibles... ``pgm``\ntries to do in 1 simple command line what you would in several.\n\nThis is code is alpha software and contains known bugs. Use at your\nown risk.\n\n\nInstallation\n============\n\n\nGetting the binary\n------------------\n\n``pgm`` requires very few dependencies. It uses ``pv`` and ``buffer``.\n\nYou can install it and his dependency by using::\n\n     sudo apt-get install -y buffer pv \u0026\u0026\n     sudo wget https://github.com/0k/pgm/raw/master/bin/pgm -O /usr/local/bin/pgm \u0026\u0026\n     sudo chmod +x /usr/local/bin/pgm\n\n\nSetup hosts\n-----------\n\n\nPassword-less access\n''''''''''''''''''''\n\nYou must have ``postgres`` and ``ssh`` server installed and running of\ncourse on each host you need to join.\n\nTo avoid typing passwords, you should use and setup each of your\ntarget hosts with password less authentification (key pair\nauthentification for instance).\n\nBy default, you could make sure your system user on the destination\nhost can ``sudo -u postgres`` without password. You could add this\nline to ``/etc/sudoers`` for instance::\n\n    bob    ALL=(postgres) NOPASSWD: /usr/bin/psql, /usr/bin/pg_dump, /usr/bin/createdb\n\nBut you can also create/edit a file ``~/.pgm.rc`` and set variable\n``prefix_pg_local_command`` (which is by default ``sudo -u postgres``) to any\nother prefix command. All commands related to postgres (``psql``, ``pg_dump``,\n``createdb``, ``dropdb``) will be prefixed by the value of this variable. A\ncommon value could be the empty string if your local user has been declared\nin postgres as a valid role.\n\nA good test would then to try (directly on the target host)::\n\n    sudo -u postgres psql -l\n\nThen try to issue the same command from the controling host::\n\n    ssh myuser@target_host \"sudo -u postgres psql -l\"\n\n\nDatabase Backup folder permissions\n''''''''''''''''''''''''''''''''''\n\nThe ``saving`` mecanism of ``pgm`` (for example ``pgm save`` or the automatic\nbackup of ``-b`` option of ``pgm copy`` or ``pgm rm``) requires you to have\ncreated a ``/var/backups/pg`` directory writeable by your user. To set it\nup your could::\n\n    mkdir /var/backups/pg -p \u0026\u0026\n    chown \"$UID\" /var/backups/pg \u0026\u0026\n    chmod 700 /var/backups/pg\n\n\nSecurity concerns\n'''''''''''''''''\n\nYou should understand all the implication of these before doing\nit. You are giving this user and all the hosts that can connect to\nthis host a password less (but protected with key pairs) access. This\nmeans that if the host that is able to join the others is compromised,\nyou'll offer the keys to all your networked postgres instances.\n\nUSE AT YOUR OWN RISK.\n\n\nLimitation\n----------\n\nIn the current state, ``pgm`` permission features (``pgm chown`` or\n``pgm cp srcdb foo@db``) are limited to full base ownership by one\nmember. So if you have any more complex scenario, please contribute.\n\n\nQuick tour\n==========\n\n\nGetting help\n------------\n\n``pgm`` supports it's own integrated help::\n\n    $ pgm --help\n    usage:\n      pgm chown [-v] USER DBNAME\n      pgm cp [-q] [-f | -b] SRC_DBNAME DST_DBNAME\n      pgm kill [-v] DBNAME\n      pgm ls [-s] [-c] [DBPATTERN]\n      pgm rm [-f] [-b] DBNAME\n      pgm save [-v] DBNAME [LABEL]\n\n\nEach command can be queried deeper::\n\n    $ pgm cp --help\n    Copy database SRC to DST database\n\n    usage:\n      pgm cp [-q] [-f | -b] SRC_DBNAME DST_DBNAME\n\n    options:\n      -q       Use quicker templating system to make the copy, note that\n               all active session will be dropped.\n      -f       Force copy even if DST_DBNAME already exists (it'll overwrite it).\n      -b       As 'force' but will backup the database before overwriting it.\n    $\n\nThe dbpattern\n-------------\n\nMost of the command will use a database pattern syntax to target a\ndatabase. This is the general syntax:\n\n    [[USER@]HOST:][PGUSER@][DBPATTERN][:PGPORT]\n\nThere are some examples::\n\n    mydb            ## a local database named ``mydb``\n    host1:mydb      ## a database ``mydb`` on the host ``host1``\n    host2:bob@mydb  ## target a database name/user, usefull for specifying destination for ``cp`` or filtering ``ls``\n\n``USER`` and ``HOST`` will be used directly by ssh. You can use anything that\nssh will understand (IP, resolvable domain name, ssh aliases...).\n\n``PGPORT`` is still not implemented. If you are under debian, you can\nprobably use ``pg_local_opts`` environment variable to set ``--cluster\n9.1/main`` option to be added to each commands, but this remains to be\nclarified, both in implementation and documentation.\n\n\nListing databases\n-----------------\n\nBy default, the ``ls`` command will list local database, along with\ntheir owner and their sizes::\n\n   $ pgm ls\n   postgres                 postgres           6540 kB\n   dbA                      bob                  32 MB\n\nYou can list distant databases::\n\n   $ pgm ls host1:\n   dev61                    openerp              37 MB\n   dummy                    openerp              32 MB\n   postgres                 postgres           5320 kB\n   test                     bob                 203 MB\n\nAnd even filter by user::\n\n   $ pgm ls host1:bob@\n   test                     bob                 203 MB\n\n\nOr use wildcards in the database names to target a subselection, the\n``-c`` optional argument will ask for the number of open connection\ncurrently on the database::\n\n   $ pgm ls host1:d* -c\n   dev61                    openerp              37 MB    8\n   dummy                    openerp              32 MB    0\n\nYou can also have a very short output (practical for scripting for\ninstance), which list only the matching database names::\n\n   $ pgm ls host1:d* -s\n   dev61\n   dummy\n\n\nCopying databases\n-----------------\n\nCopy ``dbA`` on to ``dbB``... will use templating automatically to go\nquicker, if both database are on the same server, and if there's no\nopen connection to ``dbA``::\n\n    $ pgm cp dbA dbB\n    Quick copy of dbA to dbB.\n\nor, if ``dbA`` has open connections::\n\n    $ pgm cp dbA dbB\n    Copy of dbA to dbB.\n     received: 4.43MB 0:00:03 [ 1.4MB/s] [      \u003c=\u003e             ]\n     unpacked: 4.43MB 0:00:03 [1.44MB/s] [      \u003c=\u003e             ]\n         fill: 4.26MB 0:01:03 [9.88kB/s] [               \u003c=\u003e    ]\n    Finished copy successfully.\n    $\n\nEither source or destination or both support distant databases, so you can\nuse ``pgm cp`` for exporting purpose::\n\n    $ pgm cp dbA host:dbA\n\nor importing purpose, notice the ``-f`` to force overwriting destination::\n\n    $ pgm cp -f host2:dbA dbA\n\nYou can also force ownership of destination database by using\n``\u003cowner\u003e@\u003cdbname\u003e`` syntax::\n\n    $ pgm cp dbA host1:alice@dbC\n    ...\n    Chowning dbC on host1 to user alice.\n\nNote that in case of different version of postgres, a warning will be\nissued. For instance::\n\n    Warning: Postgres version mismatch between hosts (src: 8.4.22, dst: 9.4rc1). This might generate errors !\n\n\nOther commands\n--------------\n\nDocumentation is still to be done for those::\n\n      pgm chown [-v] USER DBNAME\n      pgm kill [-v] DBNAME\n      pgm rm [-f] [-b] DBNAME\n      pgm save [-v] DBNAME [LABEL]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0k%2Fpgm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0k%2Fpgm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0k%2Fpgm/lists"}