Skip to content
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

Check if Postgres database exists prior to creation #49746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fheinecke
Copy link
Contributor

Currently, the auth server will fail to start with postgres backend if the Teleport postgres user does not have access to the postgres database. This attempts to remove this hard requirement by attempting to connect to the configuration-specified database first. This validates that the database exists, negating the need to attempt to create it, which in turn removes the need for postgres database access.

There are no tests currently covering this package, and I'm not sure how to effectively add them.

Fixes #49745

changelog: Removed the need for postgres database access when the required databases for the Postgres backend already exist

@fheinecke
Copy link
Contributor Author

If this change looks reasonable then I'll cut a dev build and test with that.

@espadolini
Copy link
Contributor

How does a function that doesn't return an error prevent Teleport from starting?

// Performs a best-effort check to see if the specified database exists. It will return 'true' if the
// database is guaranteed to exist, false otherwise. An error will only be returned if it is known to
// not stem from the database not existing.
func CheckIfDatabaseExists(ctx context.Context, poolConfig *pgxpool.Config, log *slog.Logger) (bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would unexport this until such a time that it needs to be used in other packages.


conn, err := pgx.ConnectConfig(ctx, poolConfig.ConnConfig)
if err != nil {
errMsg := fmt.Sprintf("Failed to verify that the %q database already exists",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below - use structured log fields instead of sprintf.

@marcoandredinis marcoandredinis removed their request for review December 18, 2024 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auth server crashes on initial setup when using Postgresql backend without superuser access
3 participants