@@ -413,7 +413,16 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
413
413
if ! vals .InMemoryDatabase && vals .PostgresURL == "" {
414
414
var closeFunc func ()error
415
415
cliui .Infof (inv .Stdout ,"Using built-in PostgreSQL (%s)" ,config .PostgresPath ())
416
- pgURL ,closeFunc ,err := startBuiltinPostgres (ctx ,config ,logger ,cacheDir )
416
+ customPostgresCacheDir := ""
417
+ // By default, built-in PostgreSQL will use the Coder root directory
418
+ // for its cache. However, when a deployment is ephemeral, the root
419
+ // directory is wiped clean on shutdown, defeating the purpose of using
420
+ // it as a cache. So here we use a cache directory that will not get
421
+ // removed on restart.
422
+ if vals .EphemeralDeployment .Value () {
423
+ customPostgresCacheDir = cacheDir
424
+ }
425
+ pgURL ,closeFunc ,err := startBuiltinPostgres (ctx ,config ,logger ,customPostgresCacheDir )
417
426
if err != nil {
418
427
return err
419
428
}
@@ -1964,7 +1973,7 @@ func embeddedPostgresURL(cfg config.Root) (string, error) {
1964
1973
return fmt .Sprintf ("postgres://coder@localhost:%s/coder?sslmode=disable&password=%s" ,pgPort ,pgPassword ),nil
1965
1974
}
1966
1975
1967
- func startBuiltinPostgres (ctx context.Context ,cfg config.Root ,logger slog.Logger ,cacheDir string ) (string ,func ()error ,error ) {
1976
+ func startBuiltinPostgres (ctx context.Context ,cfg config.Root ,logger slog.Logger ,customCacheDir string ) (string ,func ()error ,error ) {
1968
1977
usr ,err := user .Current ()
1969
1978
if err != nil {
1970
1979
return "" ,nil ,err
@@ -1992,8 +2001,8 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
1992
2001
}
1993
2002
1994
2003
cachePath := filepath .Join (cfg .PostgresPath (),"cache" )
1995
- if cacheDir != "" {
1996
- cachePath = filepath .Join (cacheDir ,"postgres" )
2004
+ if customCacheDir != "" {
2005
+ cachePath = filepath .Join (customCacheDir ,"postgres" )
1997
2006
}
1998
2007
stdlibLogger := slog .Stdlib (ctx ,logger .Named ("postgres" ),slog .LevelDebug )
1999
2008
ep := embeddedpostgres .NewDatabase (