Adjust server-side backup to depend on pg_write_server_files.
authorRobert Haas <rhaas@postgresql.org>
Fri, 28 Jan 2022 17:26:33 +0000 (12:26 -0500)
committerRobert Haas <rhaas@postgresql.org>
Fri, 28 Jan 2022 17:31:40 +0000 (12:31 -0500)
I had made it depend on superuser, but that seems clearly inferior.
Also document the permissions requirement in the straming replication
protocol section of the documentation, rather than only in the
section having to do with pg_basebackup.

Idea and patch from Dagfinn Ilmari MannsÃ¥ker.

Discussion: http://postgr.es/m/87bkzw160u.fsf@wibble.ilmari.org

doc/src/sgml/protocol.sgml
doc/src/sgml/ref/pg_basebackup.sgml
src/backend/replication/basebackup_server.c

index 68908dcb7b33788c48c985e2afc7bdd1e62fc31f..24e93f9b28476d6424382be3cdbcba78dc940571 100644 (file)
@@ -2647,6 +2647,11 @@ The commands accepted in replication mode are:
           <literal>blackhole</literal>, the backup data is not sent
           anywhere; it is simply discarded.
          </para>
+
+         <para>
+          The <literal>server</literal> target requires superuser privilege or
+          being granted the <literal>pg_write_server_files</literal> role.
+         </para>
         </listitem>
        </varlistentry>
 
index dfd8aebc9a3631a7fc32c9105daf9eed040a288a..1546f10c0d9e6e9f993352225d75cd25204648d3 100644 (file)
@@ -237,7 +237,8 @@ PostgreSQL documentation
         <literal>server:/some/path</literal>, the backup will be stored on
         the machine where the server is running in the
         <literal>/some/path</literal> directory. Storing a backup on the
-        server requires superuser privileges. If the target is set to
+        server requires superuser privileges or being granted the
+        <literal>pg_write_server_files</literal> role. If the target is set to
         <literal>blackhole</literal>, the contents are discarded and not
         stored anywhere. This should only be used for testing purposes, as you
         will not end up with an actual backup.
index ce1b7b47977b62ff43b67124b0d1d03699451b8a..18b0e11d903b2768274768f117bc1078388fc661 100644 (file)
  */
 #include "postgres.h"
 
+#include "catalog/pg_authid.h"
 #include "miscadmin.h"
 #include "replication/basebackup.h"
 #include "replication/basebackup_sink.h"
 #include "storage/fd.h"
+#include "utils/acl.h"
 #include "utils/timestamp.h"
 #include "utils/wait_event.h"
 
@@ -65,10 +67,10 @@ bbsink_server_new(bbsink *next, char *pathname)
    sink->base.bbs_next = next;
 
    /* Replication permission is not sufficient in this case. */
-   if (!superuser())
+   if (!is_member_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
        ereport(ERROR,
                (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-                errmsg("must be superuser to create server backup")));
+                errmsg("must be superuser or a member of the pg_write_server_files role to create server backup")));
 
    /*
     * It's not a good idea to store your backups in the same directory that