title: PostgreSQL: How to switch database inside cursor script
body: <p>I want to write function for postgresql 10 to grant read-only access to on all databases for the specified user (considerably should be run from superuser)</p>
  <p>But I have difficulties in database switching part. I try to switch the database inside a cursor but I get an error I can't understand. How can I correct my script?</p>
  <blockquote>
  <p>CONTEXT: PL/pgSQL function inline_code_block at EXECUTE</p>
  </blockquote>
  <pre><code>DO $$
  DECLARE query text;
  
  cursor cursor
  FOR
  SELECT '\c '||datname ||';'  from pg_database;
  BEGIN
  OPEN cursor;
  LOOP
      FETCH cursor INTO query;
      EXIT WHEN NOT FOUND;
      
      --raise notice 'Value: %',query;
      EXECUTE query;
  END LOOP;
  CLOSE cursor;
  END $$;
  </code></pre>