@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "fmt"
2020 _ "net/http/pprof"
21+ "os"
2122 "path/filepath"
2223
2324 "github.com/cockroachdb/errors"
@@ -49,9 +50,10 @@ import (
4950const (
5051 Version = "0.54.4"
5152
52- DefUserName = "postres"
53- DefUserEmail = "postgres@somewhere.com"
54- DoltgresDir = "postgres"
53+ DefUserName = "postres"
54+ DefUserEmail = "postgres@somewhere.com"
55+ DefUserEmailFmt = "%s@somewhere.com"
56+ DefaultDbNameEnvVar = "DOLTGRES_DB"
5557)
5658
5759func init () {
@@ -121,9 +123,10 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
121123 }
122124
123125 // We need a username and password for many SQL commands, so set defaults if they don't exist
126+ user , _ := auth .GetSuperUserAndPassword ()
124127 dEnv .Config .SetFailsafes (map [string ]string {
125- config .UserNameKey : DefUserName ,
126- config .UserEmailKey : DefUserEmail ,
128+ config .UserNameKey : user ,
129+ config .UserEmailKey : fmt . Sprintf ( DefUserEmailFmt , user ) ,
127130 })
128131
129132 // Reload the dolt environment with the correct data dir that was specified in the configuration.
@@ -134,19 +137,16 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
134137 }
135138 dEnv = env .Load (ctx , dEnv .GetUserHomeDir , dataDirFs , doltdb .LocalDirDoltDB , dEnv .Version )
136139
137- // Automatically initialize a doltgres database if necessary
138- // TODO: probably should only do this if there are no databases in the data dir already
139- createDoltgresDatabase := false
140- if exists , isDirectory := dataDirFs .Exists (DoltgresDir ); ! exists {
141- createDoltgresDatabase = true
142- } else if ! isDirectory {
143- workingDir , _ := dataDirFs .Abs ("." )
144- // The else branch means that there's a Doltgres item, so we need to error if it's a file since we
145- // enforce the creation of a Doltgres database/directory, which would create a name conflict with the file
146- return nil , errors .Errorf ("Attempted to create the default `postgres` database at `%s`, but a file with " +
147- "the same name was found. Either remove the file, change the directory using the `--data-dir` argument, " +
148- "or change the environment variable `%s` so that it points to a different directory." , workingDir , servercfg .DOLTGRES_DATA_DIR )
140+ // Determine whether we need to initialize the default database
141+ initializeDefaultDatabase := true
142+ mrEnv , err := env .MultiEnvForDirectory (ctx , dEnv .Config .WriteableConfig (), dataDirFs , Version , dEnv )
143+ if err != nil {
144+ return nil , err
149145 }
146+ mrEnv .Iter (func (_ string , _ * env.DoltEnv ) (stop bool , err error ) {
147+ initializeDefaultDatabase = false
148+ return true , nil
149+ })
150150
151151 controller := svcs .NewController ()
152152 newCtx , cancelF := context .WithCancel (ctx )
@@ -178,8 +178,8 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
178178 return nil , err
179179 }
180180
181- if createDoltgresDatabase {
182- err = createDatabase (ssCfg , "postgres" )
181+ if initializeDefaultDatabase {
182+ err = createDefaultDatabase (ssCfg )
183183 if err != nil {
184184 return nil , err
185185 }
@@ -194,10 +194,11 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
194194 return controller , nil
195195}
196196
197- // createDatabase creates the database named on the local server using the configuration values to connect, returning
197+ // createDefaultDatabase creates the database named on the local server using the configuration values to connect, returning
198198// any error
199- func createDatabase (cfg doltservercfg.ServerConfig , dbName string ) error {
199+ func createDefaultDatabase (cfg doltservercfg.ServerConfig ) error {
200200 user , password := auth .GetSuperUserAndPassword ()
201+ dbName := getDefaultDatabaseName (user )
201202
202203 dsn := fmt .Sprintf ("postgres://%s:%s@localhost:%d" , user , password , cfg .Port ())
203204
@@ -213,6 +214,17 @@ func createDatabase(cfg doltservercfg.ServerConfig, dbName string) error {
213214 return err
214215}
215216
217+ // getDefaultDatabaseName returns the name of the default database to create on first server start.
218+ // If the environment variable DOLTGRES_DB is set, that value is used. Otherwise, the username is used.
219+ // The username is in turn configured with the environment variable DOLTGRES_USER, defaulting to "postgres".
220+ func getDefaultDatabaseName (userName string ) string {
221+ defaultDbName := os .Getenv (DefaultDbNameEnvVar )
222+ if defaultDbName != "" {
223+ return defaultDbName
224+ }
225+ return userName
226+ }
227+
216228// startReplication begins the background thread that replicates from Postgres, if one is configured.
217229func startReplication (cfg * servercfg.DoltgresConfig , ssCfg doltservercfg.ServerConfig ) (* logrepl.LogicalReplicator , error ) {
218230 if cfg .PostgresReplicationConfig == nil {
0 commit comments