Skip to content

Instantly share code, notes, and snippets.

@ohmyhusky
Last active March 4, 2025 06:17
Show Gist options
  • Save ohmyhusky/7986bb478abf3f44db2e10f1f2e36a8c to your computer and use it in GitHub Desktop.
Save ohmyhusky/7986bb478abf3f44db2e10f1f2e36a8c to your computer and use it in GitHub Desktop.
for toolbox to fetch
const { Client } = require("pg");
async function createPostgresResources() {
const adminConfig = {
user: "",
host: "",
database: "postgres", // Initially connect to 'postgres'
password: "",
port: 5432,
};
const appUserName = "";
const appUserPassword = "";
const databaseName = "";
const schemaName = "public";
let adminClient;
try {
/* adminClient = new Client(adminConfig);
await adminClient.connect();
console.log(
'Connected to PostgreSQL as admin user (initially to "postgres" db)'
);
console.log(`Creating database: ${databaseName}`);
await adminClient.query(`CREATE DATABASE ${databaseName};`);
console.log(`Database ${databaseName} created successfully`);
console.log(`Creating user: ${appUserName}`);
await adminClient.query(
`CREATE USER ${appUserName} WITH PASSWORD '${appUserPassword}';`
);
console.log(`User ${appUserName} created successfully`);
console.log(
`Granting CONNECT privilege on DATABASE ${databaseName} to ${appUserName}`
);
await adminClient.query(
`GRANT CONNECT ON DATABASE ${databaseName} TO ${appUserName};`
);
console.log(
`CONNECT privilege on DATABASE ${databaseName} granted to ${appUserName}`
);
// **Important Change: Switch adminClient to connect to the newly created 'appdb'**
await adminClient.end(); // Close the connection to 'postgres'
adminConfig.database = databaseName; // Change the database in the config */
adminClient = new Client(adminConfig); // Create a new client with the updated config
await adminClient.connect(); // Connect to 'appdb'
console.log(`Admin client reconnected to database: ${databaseName}`);
console.log(`
GRANT ALL PRIVILEGES ON DATABASE ${databaseName} TO ${appUserName}
`);
await adminClient.query(
`GRANT ALL PRIVILEGES ON DATABASE ${databaseName} TO ${appUserName};`
);
console.log(`ALL PRIVILEGES granted to ${appUserName}`)
console.log(
`Granting ALL privilege on SCHEMA ${schemaName} to ${appUserName}`
);
await adminClient.query(
`GRANT ALL PRIVILEGES ON SCHEMA ${schemaName} TO ${appUserName};`
);
console.log(
`ALL privilege on SCHEMA ${schemaName} granted to ${appUserName}`
);
console.log(
`Granting ALL PRIVILEGES on ALL TABLES IN SCHEMA ${schemaName} to ${appUserName}`
);
await adminClient.query(
`GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${schemaName} TO ${appUserName};`
);
console.log(
`ALL PRIVILEGES on ALL TABLES in SCHEMA ${schemaName} granted to ${appUserName}`
);
/* console.log(
`Setting default privileges for future tables in SCHEMA ${schemaName} for role ${appUserName}`
);
await adminClient.query(
`ALTER DEFAULT PRIVILEGES FOR ROLE ${appUserName} IN SCHEMA ${schemaName} GRANT ALL ON TABLES TO ${appUserName};`
);
console.log(
`Default privileges set for future tables in SCHEMA ${schemaName} for role ${appUserName}`
); */
console.log(
"PostgreSQL resources created and permissions granted successfully!"
);
} catch (error) {
console.error("Error creating PostgreSQL resources:", error);
} finally {
if (adminClient) {
await adminClient.end();
console.log("Admin client connection closed");
}
}
}
// Run the function to create resources
createPostgresResources();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment