/* non-export function prototypes */
 static CopyState BeginCopy(ParseState *pstate, bool is_from, Relation rel, Node *raw_query,
-                                                  const Oid queryRelId, List *attnamelist,
+                 const Oid queryRelId, List *attnamelist,
                  List *options);
 static void EndCopy(CopyState cstate);
 static void ClosePipeToProgram(CopyState cstate);
                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                         errmsg("option \"%s\" not recognized",
                                                        defel->defname),
-                                                parser_errposition(pstate, defel->location)));
+                                        parser_errposition(pstate, defel->location)));
        }
 
        /*
                        cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
                        umask(oumask);
                        if (cstate->copy_file == NULL)
+                       {
+                               /* copy errno because ereport subfunctions might change it */
+                               int                     save_errno = errno;
+
                                ereport(ERROR,
                                                (errcode_for_file_access(),
                                                 errmsg("could not open file \"%s\" for writing: %m",
-                                                               cstate->filename)));
+                                                               cstate->filename),
+                                                (save_errno == ENOENT || save_errno == EACCES) ?
+                                                errhint("COPY TO instructs the PostgreSQL server process to write a file. "
+                                                                "You may want a client-side facility such as psql's \\copy.") : 0));
+                       }
 
                        if (fstat(fileno(cstate->copy_file), &st))
                                ereport(ERROR,
 
                        cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R);
                        if (cstate->copy_file == NULL)
+                       {
+                               /* copy errno because ereport subfunctions might change it */
+                               int                     save_errno = errno;
+
                                ereport(ERROR,
                                                (errcode_for_file_access(),
                                                 errmsg("could not open file \"%s\" for reading: %m",
-                                                               cstate->filename)));
+                                                               cstate->filename),
+                                                (save_errno == ENOENT || save_errno == EACCES) ?
+                                                errhint("COPY FROM instructs the PostgreSQL server process to read a file. "
+                                                                "You may want a client-side facility such as psql's \\copy.") : 0));
+                       }
 
                        if (fstat(fileno(cstate->copy_file), &st))
                                ereport(ERROR,