-
Notifications
You must be signed in to change notification settings - Fork 5k
[postgres] Showcase Postgresql monitoring with dedicated user and the role pg_monitor
#2665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,42 @@ | ||
| -- Copyright The OpenTelemetry Authors | ||
| -- SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| CREATE USER otelu WITH PASSWORD 'otelp'; | ||
| -- Create the shop_db database if it does not exist (uses psql \gexec) | ||
| SELECT 'CREATE DATABASE shop_db' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'shop_db')\gexec | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create the |
||
|
|
||
| -- Create shop_user (idempotent) | ||
| DO | ||
| $$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'shop_user') THEN | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, |
||
| CREATE ROLE shop_user LOGIN PASSWORD 'shop_password'; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| -- Create a table | ||
| CREATE TABLE "order" ( | ||
| -- Ensure monitoring_user exists (idempotent) | ||
| DO | ||
| $$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'monitoring_user') THEN | ||
| CREATE ROLE monitoring_user LOGIN PASSWORD 'monitoring_password'; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| -- Give connect privileges on the database to the users | ||
| GRANT CONNECT ON DATABASE shop_db TO shop_user; | ||
| GRANT CONNECT ON DATABASE shop_db TO monitoring_user; | ||
|
|
||
| -- Switch to the shop_db database to create tables and grant schema/table privileges | ||
| \connect shop_db | ||
|
|
||
| -- Create tables inside shop_db (idempotent) | ||
| CREATE TABLE IF NOT EXISTS "order" ( | ||
| order_id TEXT PRIMARY KEY | ||
| ); | ||
|
|
||
| CREATE TABLE shipping ( | ||
| CREATE TABLE IF NOT EXISTS shipping ( | ||
| shipping_tracking_id TEXT PRIMARY KEY, | ||
| shipping_cost_currency_code TEXT NOT NULL, | ||
| shipping_cost_units BIGINT NOT NULL, | ||
|
|
@@ -23,7 +50,7 @@ CREATE TABLE shipping ( | |
| FOREIGN KEY (order_id) REFERENCES "order"(order_id) ON DELETE CASCADE | ||
| ); | ||
|
|
||
| CREATE TABLE orderitem ( | ||
| CREATE TABLE IF NOT EXISTS orderitem ( | ||
| item_cost_currency_code TEXT NOT NULL, | ||
| item_cost_units BIGINT NOT NULL, | ||
| item_cost_nanos INT NOT NULL, | ||
|
|
@@ -34,4 +61,13 @@ CREATE TABLE orderitem ( | |
| FOREIGN KEY (order_id) REFERENCES "order"(order_id) ON DELETE CASCADE | ||
| ); | ||
|
|
||
| GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO otelu; | ||
| -- Grant read/write privileges on existing and future tables to shop_user | ||
| GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO shop_user; | ||
| ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE ON TABLES TO shop_user; | ||
|
|
||
| -- Grant monitoring privileges to monitoring_user | ||
| GRANT pg_monitor TO monitoring_user; | ||
| GRANT USAGE ON SCHEMA public TO monitoring_user; | ||
| GRANT SELECT ON ALL TABLES IN SCHEMA public TO monitoring_user; | ||
| ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO monitoring_user; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep the default database instance
postgresand create a dedicated DB instanceshop_dbfor the application, seeinit.sqlfor the creation of thisshop_dbinstance.