PageSourceSearch

https://chat2db.ai/_next/static/chunks/pages/tools/postgres-grant-generator-97c9b4623c63969f.js

js chat2db.ai collected 2026-09-28 10:20:22 UTC 15,972 bytes, 1 lines download raw bytes

1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[4657],{1250:function(e,t,a){(window.__NEXT_P=window.__NEXT_P||[]).push(["/tools/postgres-grant-generator",function(){return a(5819)}])},9824:function(e,t,a){"use strict";a.d(t,{A1:function(){return p},PU:function(){return x},V4:function(){return m}});var s=a(2676),n=a(5271),l=a(137),c=a.n(l),r=a(9965),o=a.n(r),i=a(4032),d=a(1831),h=a(3349),E=a(2427),u=a(6904);let p=e=>{let{label:t,value:a,onChange:l,placeholder:c,readOnly:r,rows:o=10}=e,[i,d]=(0,n.useState)(!1);return(0,s.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-2",children:[(0,s.jsx)("label",{className:"text-sm font-semibold text-[#B8AFCA]",children:t}),r&&(0,s.jsx)("button",{type:"button",className:"text-xs px-3 py-1 rounded-full border border-[#3E30A8] hover:bg-[#3E30A8] transition-colors",onClick:()=>{navigator.clipboard.writeText(a),d(!0),setTimeout(()=>d(!1),1500)},children:i?"Copied!":"Copy"})]}),(0,s.jsx)("textarea",{className:"w-full rounded-[12px] bg-[#16151A] border border-[#2A2440] focus:border-[#3E30A8] focus:outline-none p-4 font-mono text-sm text-white",rows:o,value:a,onChange:e=>null==l?void 0:l(e.target.value),placeholder:c,readOnly:r,spellCheck:!1})]})},m=e=>{let{onClick:t,children:a}=e;return(0,s.jsx)("button",{type:"button",onClick:t,className:"px-6 py-2 rounded-full bg-[#3E30A8] hover:bg-[#4E40C8] transition-colors font-semibold",children:a})},x=e=>{let{label:t,value:a,onChange:n,options:l}=e;return(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:[t,(0,s.jsx)("select",{className:"bg-[#16151A] border border-[#2A2440] rounded-[8px] px-2 py-1 text-white",value:a,onChange:e=>n(e.target.value),children:l.map(e=>(0,s.jsx)("option",{value:e.value,children:e.label},e.value))})]})};t.ZP=e=>{let{slug:t,title:a,description:n,h1:l,intro:r,children:p,howTo:m,faq:x,afterTool:A}=e,N=u.K.filter(e=>e.slug!==t);return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(h.Z,{title:a,description:n}),x&&x.length>0&&(0,s.jsx)(c(),{children:(0,s.jsx)("script",{type:"application/ld+json",dangerouslySetInnerHTML:{__html:JSON.stringify({"@context":"https://schema.org","@type":"FAQPage",mainEntity:x.map(e=>({"@type":"Question",name:e.q,acceptedAnswer:{"@type":"Answer",text:e.a}}))})}})}),(0,s.jsxs)("div",{className:"min-h-screen flex flex-col pt-[80px] bg-black text-white",children:[(0,s.jsx)(i.Z,{}),(0,s.jsxs)("main",{className:"w-full max-w-5xl mx-auto px-4 md:px-8 flex-1",children:[(0,s.jsxs)("header",{className:"pt-10 pb-6 text-center",children:[(0,s.jsx)("h1",{className:"font-archivo-black text-3xl md:text-5xl font-bold mb-4",children:l}),(0,s.jsx)("p",{className:"text-[#9289A4] max-w-3xl mx-auto",children:r})]}),(0,s.jsx)("section",{className:"mb-10",children:p}),(0,s.jsxs)("section",{className:"my-12 rounded-[20px] border border-[#3E30A8] bg-[#16151A] p-8 text-center",children:[(0,s.jsxs)("h2",{className:"text-2xl font-bold mb-3",children:["Do more than ",l.toLowerCase()," — meet Chat2DB"]}),(0,s.jsx)("p",{className:"text-[#9289A4] max-w-2xl mx-auto mb-6",children:"Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace."}),(0,s.jsxs)("div",{className:"flex gap-4 justify-center flex-wrap",children:[(0,s.jsx)(o(),{href:"/download",children:(0,s.jsx)(E.Z,{variant:"primary",children:"Download Chat2DB Free"})}),(0,s.jsx)("a",{href:"https://app.chat2db.ai",target:"_blank",rel:"noopener noreferrer",children:(0,s.jsx)(E.Z,{variant:"secondary",children:"Try Chat2DB Web"})})]})]}),A,m&&m.length>0&&(0,s.jsxs)("section",{className:"my-10",children:[(0,s.jsx)("h2",{className:"text-2xl font-bold mb-4",children:"How to use"}),(0,s.jsx)("ol",{className:"list-decimal list-inside space-y-2 text-[#B8AFCA]",children:m.map((e,t)=>(0,s.jsx)("li",{children:e},t))})]}),x&&x.length>0&&(0,s.jsxs)("section",{className:"my-10",children:[(0,s.jsx)("h2",{className:"text-2xl font-bold mb-4",children:"Frequently asked questions"}),(0,s.jsx)("div",{className:"space-y-6",children:x.map((e,t)=>(0,s.jsxs)("div",{children:[(0,s.jsx)("h3",{className:"text-lg font-semibold mb-1",children:e.q}),(0,s.jsx)("p",{className:"text-[#9289A4]",children:e.a})]},t))})]}),(0,s.jsxs)("section",{className:"my-12",children:[(0,s.jsx)("h2",{className:"text-2xl font-bold mb-4",children:"More free SQL tools"}),(0,s.jsx)("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:N.map(e=>(0,s.jsxs)(o(),{href:"/tools/".concat(e.slug),className:"block p-4 rounded-[12px] border border-[#2A2440] hover:border-[#4A4460] transition-colors",children:[(0,s.jsx)("div",{className:"font-semibold mb-1",children:e.name}),(0,s.jsx)("div",{className:"text-sm text-[#9289A4]",children:e.short})]},e.slug))})]})]}),(0,s.jsx)(d.Z,{})]})]})}},5819:function(e,t,a){"use strict";a.r(t),a.d(t,{__N_SSP:function(){return x}});var s=a(2676),n=a(5271),l=a(9824);let c=["SELECT","INSERT","UPDATE","DELETE","TRUNCATE","REFERENCES","TRIGGER"],r=["USAG
1E","SELECT","UPDATE"],o=[{value:"readonly",label:"Read-only (SELECT)"},{value:"readwrite",label:"Read-write (SELECT/INSERT/UPDATE/DELETE)"},{value:"full",label:"Full access (ALL PRIVILEGES)"},{value:"custom",label:"Custom"}],i=e=>"readonly"===e?["SELECT"]:"readwrite"===e?["SELECT","INSERT","UPDATE","DELETE"]:"full"===e?[...c]:["SELECT"],d=e=>"readonly"===e?["SELECT"]:"readwrite"===e?["USAGE","SELECT"]:"full"===e?[...r]:["USAGE","SELECT"],h=e=>{let t=e.trim();return t?/^[a-z_][a-z0-9_]*$/.test(t)?t:'"'.concat(t.replace(/"/g,'""'),'"'):t},E=e=>"'".concat(e.replace(/'/g,"''"),"'"),u=e=>e.split(",").map(e=>e.trim()).filter(Boolean),p="bg-[#16151A] border border-[#2A2440] rounded-[8px] px-3 py-1.5 text-white focus:border-[#3E30A8] focus:outline-none font-mono text-sm",m=e=>{let{label:t,checked:a,onChange:n}=e;return(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA] cursor-pointer",children:[(0,s.jsx)("input",{type:"checkbox",checked:a,onChange:e=>n(e.target.checked)}),t]})};var x=!0;t.default=()=>{let[e,t]=(0,n.useState)("app_reader"),[a,x]=(0,n.useState)(!0),[A,N]=(0,n.useState)("change_me"),[g,b]=(0,n.useState)("mydb"),[T,S]=(0,n.useState)("public"),[L,f]=(0,n.useState)("readonly"),[C,R]=(0,n.useState)(i("readonly")),[O,j]=(0,n.useState)(d("readonly")),[v,w]=(0,n.useState)(!1),[I,y]=(0,n.useState)(!1),[G,F]=(0,n.useState)(""),[P,U]=(0,n.useState)(!0),[_,D]=(0,n.useState)(""),[B,M]=(0,n.useState)(!1),[k,H]=(0,n.useState)(!0),V=(e,t,a)=>{f("custom"),t(e.includes(a)?e.filter(e=>e!==a):[...e,a])},q=(0,n.useMemo)(()=>{let t=h(e)||"app_user",s=h(g)||"mydb",n=h(T)||"public",l=B?" WITH GRANT OPTION":"",o=u(G).map(e=>e.includes(".")?e:"".concat(n,".").concat(h(e))),i=C.length===c.length?"ALL PRIVILEGES":C.join(", "),d=O.length===r.length?"ALL PRIVILEGES":O.join(", "),p=[];if(p.push("-- Run as a superuser or as the owner of the objects (e.g. psql -U postgres -d ".concat(g||"mydb",")")),a&&(p.push("",'-- 1. Create the role (LOGIN makes it a "user")'),p.push("CREATE ROLE ".concat(t," WITH LOGIN PASSWORD ").concat(E(A||"change_me"),";"))),p.push("","-- 2. Allow the role to connect to the database"),p.push("GRANT CONNECT ON DATABASE ".concat(s," TO ").concat(t,";")),p.push("","-- 3. Schema access: USAGE is required before any table privilege works"),p.push("GRANT USAGE".concat(I?", CREATE":""," ON SCHEMA ").concat(n," TO ").concat(t).concat(l,";")),C.length>0&&(p.push("","-- 4. Table privileges"),o.length>0?p.push("GRANT ".concat(i," ON TABLE ").concat(o.join(", ")," TO ").concat(t).concat(l,";")):p.push("GRANT ".concat(i," ON ALL TABLES IN SCHEMA ").concat(n," TO ").concat(t).concat(l,";"))),O.length>0&&0===o.length&&(p.push("","-- 5. Sequences (needed for serial / identity columns on INSERT)"),p.push("GRANT ".concat(d," ON ALL SEQUENCES IN SCHEMA ").concat(n," TO ").concat(t).concat(l,";"))),v&&0===o.length&&(p.push("","-- 6. Functions and procedures"),p.push("GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA ".concat(n," TO ").concat(t).concat(l,";"))),P&&0===o.length){let e=_.trim()?" FOR ROLE ".concat(h(_)):"";p.push("","-- 7. Future objects: default privileges apply to objects created later".concat(_.trim()?" by ".concat(h(_)):" by the role running this script (add FOR ROLE <owner> if another role creates tables)")),C.length>0&&p.push("ALTER DEFAULT PRIVILEGES".concat(e," IN SCHEMA ").concat(n," GRANT ").concat(i," ON TABLES TO ").concat(t).concat(l,";")),O.length>0&&p.push("ALTER DEFAULT PRIVILEGES".concat(e," IN SCHEMA ").concat(n," GRANT ").concat(d," ON SEQUENCES TO ").concat(t).concat(l,";")),v&&p.push("ALTER DEFAULT PRIVILEGES".concat(e," IN SCHEMA ").concat(n," GRANT EXECUTE ON FUNCTIONS TO ").concat(t).concat(l,";"))}let m=["-- Check what ".concat(t," can do"),"\\du ".concat(e||"app_user"),"\\dp ".concat(T||"public",".*"),"SELECT has_schema_privilege(".concat(E(e||"app_user"),", ").concat(E(T||"public"),", 'USAGE');"),"SELECT table_schema, table_name, privilege_type","FROM information_schema.role_table_grants","WHERE grantee = ".concat(E(e||"app_user")),"ORDER BY 1, 2, 3;"].join("\n"),x=["-- Undo everything above (run before DROP ROLE)","ALTER DEFAULT PRIVILEGES IN SCHEMA ".concat(n," REVOKE ALL ON TABLES FROM ").concat(t,";"),"ALTER DEFAULT PRIVILEGES IN SCHEMA ".concat(n," REVOKE ALL ON SEQUENCES FROM ").concat(t,";"),"ALTER DEFAULT PRIVILEGES IN SCHEMA ".concat(n," REVOKE ALL ON FUNCTIONS FROM ").concat(t,";"),"REVOKE ALL ON ALL FUNCTIONS IN SCHEMA ".concat(n," FROM ").concat(t,";"),"REVOKE ALL ON ALL SEQUENCES IN SCHEMA ".concat(n," FROM ").concat(t,";"),"REVOKE ALL ON ALL TABLES IN SCHEMA ".concat(n," FROM ").concat(t,";"),"REVOKE ALL ON SCHEMA ".concat(n," FROM ").concat(t,";"),"REVOKE CONNECT ON DATABASE ".concat(s," FROM ").concat(t,";"),"-- If the role owns objects or has grants in other databases:","-- REASSIGN OWNED BY ".concat(t," TO postgres; DROP OWNED BY ").concat(t,";"),"DROP ROLE ".concat(t,";")].join("\n");return{sql:p.join("\n"),verify:m,revoke:x}}
1,[e,a,A,g,T,C,O,v,I,G,P,_,B]);return(0,s.jsx)(l.ZP,{slug:"postgres-grant-generator",title:"Postgres GRANT Generator – Privileges & Roles",description:"Generate PostgreSQL GRANT statements online: create a read-only or read-write user and grant privileges on database, schema, all tables and sequences.",h1:"Postgres GRANT Statement Generator",intro:"Pick a role, database, schema and privilege preset and get a complete, correctly ordered PostgreSQL permission script: CREATE ROLE, GRANT CONNECT ON DATABASE, GRANT USAGE ON SCHEMA, GRANT ... ON ALL TABLES / SEQUENCES / FUNCTIONS IN SCHEMA, plus ALTER DEFAULT PRIVILEGES so tables created later are covered too. Matching verification queries and a REVOKE script are generated alongside. Everything runs in your browser — nothing is sent to a server.",howTo:["Enter the role name, database and schema, then choose a preset: read-only, read-write, full access, or tick individual privileges.","Optionally restrict the grant to specific tables, add WITH GRANT OPTION, or set the owner role for ALTER DEFAULT PRIVILEGES.","Copy the GRANT script and run it as a superuser or the object owner; use the verification queries to confirm, and the REVOKE script to undo."],faq:[{q:"Why does GRANT ALL PRIVILEGES ON DATABASE not give access to tables?",a:"Database-level privileges in PostgreSQL are only CONNECT, CREATE (schemas) and TEMP. Tables live inside schemas, so a user also needs USAGE on the schema and SELECT/INSERT/... on the tables themselves — usually via GRANT ... ON ALL TABLES IN SCHEMA. This generator emits all three layers in the right order, which is why the typical 'permission denied for table' error disappears."},{q:"How do I grant privileges on tables that will be created in the future?",a:"GRANT ... ON ALL TABLES IN SCHEMA only affects tables that exist right now. For future tables use ALTER DEFAULT PRIVILEGES IN SCHEMA schema GRANT SELECT ON TABLES TO role. Note that default privileges apply to objects created by the role that ran the ALTER DEFAULT PRIVILEGES statement; if another role (for example a migration user) creates the tables, add FOR ROLE that_role, which this tool supports via the Owner role field."},{q:"How can I check which privileges a PostgreSQL user has?",a:"In psql, \\du lists roles and attributes, \\dp schema.* shows table/sequence ACLs, and \\ddp shows default privileges. In SQL, query information_schema.role_table_grants or call has_table_privilege('role', 'schema.table', 'SELECT') and has_schema_privilege('role', 'schema', 'USAGE'). If you prefer a GUI, Chat2DB — a free AI-powered database client — lets you browse roles and run these checks visually; download it at https://chat2db.ai/download or use the web version at https://app.chat2db.ai."}],children:(0,s.jsxs)("div",{className:"flex flex-col gap-4",children:[(0,s.jsxs)("div",{className:"flex flex-wrap gap-4 items-center",children:[(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Role / user",(0,s.jsx)("input",{className:"".concat(p," w-40"),value:e,onChange:e=>t(e.target.value)})]}),(0,s.jsx)(m,{label:"CREATE ROLE with LOGIN",checked:a,onChange:x}),a&&(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Password",(0,s.jsx)("input",{className:"".concat(p," w-36"),value:A,onChange:e=>N(e.target.value)})]}),(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Database",(0,s.jsx)("input",{className:"".concat(p," w-32"),value:g,onChange:e=>b(e.target.value)})]}),(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Schema",(0,s.jsx)("input",{className:"".concat(p," w-32"),value:T,onChange:e=>S(e.target.value)})]})]}),(0,s.jsxs)("div",{className:"flex flex-wrap gap-4 items-center",children:[(0,s.jsx)(l.PU,{label:"Preset",value:L,onChange:e=>{f(e),"custom"!==e&&(R(i(e)),j(d(e)),w("readonly"!==e),y("full"===e))},options:o}),(0,s.jsx)(m,{label:"WITH GRANT OPTION",checked:B,onChange:M}),(0,s.jsx)(m,{label:"CREATE on schema",checked:I,onChange:e=>{f("custom"),y(e)}}),(0,s.jsx)(m,{label:"EXECUTE on functions",checked:v,onChange:e=>{f("custom"),w(e)}})]}),(0,s.jsxs)("div",{className:"flex flex-wrap gap-x-6 gap-y-2 items-center",children:[(0,s.jsx)("span",{className:"text-sm text-[#B8AFCA]",children:"Tables:"}),c.map(e=>(0,s.jsx)(m,{label:e,checked:C.includes(e),onChange:()=>V(C,R,e)},e))]}),(0,s.jsxs)("div",{className:"flex flex-wrap gap-x-6 gap-y-2 items-center",children:[(0,s.jsx)("span",{className:"text-sm text-[#B8AFCA]",children:"Sequences:"}),r.map(e=>(0,s.jsx)(m,{label:e,checked:O.includes(e),onChange:()=>V(O,j,e)},e))]}),(0,s.jsxs)("div",{className:"flex flex-wrap gap-4 items-center",children:[(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Specific tables (comma-separated, empty = ALL TABLES IN SCHEMA)",(0,s.jsx)("input",{className:"".concat(p," w-64"),value:G,onChange:e=>F(e.target.value),placeholder:"orders, customers"})]}),(0,s.jsx)(m,{label:"ALTER DEFAULT PRIVILEGES (future objects)",checked:P,onChange:U}),P&&(0,s.jsxs)("label",{className:"flex items-center gap-2 text-sm text-[#B8AFCA]",children:["Owner role (FOR ROLE, optional)",(0,s.jsx)("input",{className:"".concat(p," w-36"),value:_,onCh
1ange:e=>D(e.target.value),placeholder:"migrator"})]}),(0,s.jsx)(m,{label:"Show REVOKE script",checked:k,onChange:H})]}),(0,s.jsx)(l.A1,{label:"GRANT script",value:q.sql,readOnly:!0,rows:22}),(0,s.jsxs)("div",{className:"flex flex-col md:flex-row gap-4",children:[(0,s.jsx)(l.A1,{label:"Verify privileges (psql)",value:q.verify,readOnly:!0,rows:10}),k&&(0,s.jsx)(l.A1,{label:"REVOKE / cleanup script",value:q.revoke,readOnly:!0,rows:12})]})]})})}}},function(e){e.O(0,[9257,9045,5812,7799,2888,9774,179],function(){return e(e.s=1250)}),_N_E=e.O()}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.