Skip to content

Commit

Permalink
update postgres create user function
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Nov 27, 2023
1 parent 7ee478c commit 5055727
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions cmd/substreams-sink-sql/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

var createUserCmd = Command(createUserE,
"create-user <dsn> <username>",
"create-user <dsn> <username> <database>",
"Create a user in the database",
ExactArgs(2),
ExactArgs(3),
Flags(func(flags *pflag.FlagSet) {
flags.Int("retries", 3, "Number of retries to attempt when a connection error occurs")
flags.Bool("read-only", false, "Create a read-only user")
Expand All @@ -30,6 +30,7 @@ func createUserE(cmd *cobra.Command, args []string) error {

dsn := args[0]
username := args[1]
database := args[2]

readOnly := sflags.MustGetBool(cmd, "read-only")
passwordEnv := sflags.MustGetString(cmd, "password-env")
Expand All @@ -49,7 +50,7 @@ func createUserE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("new psql loader: %w", err)
}

err = dbLoader.CreateUser(ctx, username, password, "substreams", readOnly)
err = dbLoader.CreateUser(ctx, username, password, database, readOnly)
if err != nil {
return fmt.Errorf("create user: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions db/dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ func (d postgresDialect) CreateUser(tx Tx, ctx context.Context, l *Loader, usern
var q string
if readOnly {
q = fmt.Sprintf(`
CREATE USER %s WITH PASSWORD '%s';
CREATE ROLE %s LOGIN PASSWORD '%s';
GRANT CONNECT ON DATABASE %s TO %s;
GRANT USAGE ON SCHEMA public TO %s;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO %s
GRANT SELECT ON ALL TABLES IN SCHEMA public TO %s;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO %s;
`, user, pass, db, user, user, user, user)
} else {
q = fmt.Sprintf("CREATE USER %s WITH PASSWORD '%s'; GRANT ALL PRIVILEGES ON DATABASE %s TO %s;", user, pass, db, user)
Expand Down

0 comments on commit 5055727

Please sign in to comment.