运行Go后端服务器时遇‘device not configured’错误求助
Go后端启动报错device not configured排查求助
运行Go后端服务器时持续遇到device not configured错误,无法启动服务。配置文件已按照文档要求设置,且PostgreSQL数据库已正常启动,但问题仍未解决。以下是相关信息:
终端输出
solomonng@Solomons-MBP cornerstone-backend % brew services start postgresql@13 Service postgresql@13 already started, use brew services restart postgresql@13 to restart. solomonng@Solomons-MBP cornerstone-backend % make run Running server in development mode... Starting service device not configured solomonng@Solomons-MBP cornerstone-backend % export GO_ENV=development && reflex -d none -r '.go$$' -s go run ./cmd/server/main.go Starting service device not configured
环境配置(env.development.local)
SERVER_PORT=8000 DB_HOSTNAME=/tmp DB_PORT=5432 DB_NAME=aac_development DB_USERNAME=solomonng DB_PASSWORD= DB_SSLMODE=
相关代码(./cmd/server/main.go)
package vms import ( "gorm.io/gorm" "gorm.io/gorm/clause" "gitlab.com/nuscvwo/aac/aac-backend/internal/errors/externalerrors" "gitlab.com/nuscvwo/aac/aac-backend/internal/metadata" "gitlab.com/nuscvwo/aac/aac-backend/pkg/enums" "gitlab.com/nuscvwo/aac/aac-backend/pkg/nullable" "gitlab.com/nuscvwo/aac/aac-backend/pkg/userstamps" ) type Organisation struct { metadata.Model userstamps.StampModel Name nullable.String `gorm:"not null"` OrganisationType nullable.Int `gorm:"not null"` Staffs []Staff `gorm:"constraint:OnDelete:CASCADE"` Students []Student `gorm:"constraint:OnDelete:CASCADE"` Deployments []Deployment `gorm:"constraint:OnDelete:CASCADE"` } func (Organisation) TableName() string { return "vms_organisations" } const ( OrgModelName = "organisation" ErrOrgNameEmpty = "Organisation name must not be empty" ErrOrgTypeEmpty = "Organisation type must not be empty" ) func (org *Organisation) Validate() error { // TODO: validation if !org.Name.Valid { return &externalerrors.ValidationError{Message: ErrOrgNameEmpty} } return nil } func (org *Organisation) ValidateMustBeNew() error { if org.ID.Valid { return &externalerrors.CreateExistingRecordError{ Model: OrgModelName, ModelID: org.ID.ValueOrZero(), } } return nil } func (org *Organisation) ensureOrganisationIsNewOrSame(originalID nullable.Int) error { if !org.ID.Valid { return org.ValidateMustBeNew() } if org.ID != originalID { return externalerrors.ValidationErrorRecordChanged( OrgModelName, originalID.ValueOrZero(), org.ID.ValueOrZero(), ) } return nil } func (org *Organisation) ValidateAssociations(original *Organisation) error { if err := org.ensureOrganisationIsNewOrSame(original.ID); err != nil { return err } return nil } func (org *Organisation) PruneOldAssociations(original *Organisation, tx *gorm.DB) error { return nil } func (org *Organisation) Create(tx *gorm.DB) error { return tx.Create(org).Error } func (org *Organisation) Update(tx *gorm.DB) error { return tx.Omit(clause.Associations).Updates(org).Error } func (org *Organisation) Delete(tx *gorm.DB) error { return tx.Delete(org).Error } func (org *Organisation) BeforeCreate(tx *gorm.DB) error { if err := org.validateEnums(tx); err != nil { return err } return org.Validate() } func (org *Organisation) BeforeUpdate(tx *gorm.DB) error { if err := org.validateEnums(tx); err != nil { return err } return org.Validate() } func (org *Organisation) validateEnums(tx *gorm.DB) error { orgTypeEnum := enums.OrganisationType(org.OrganisationType.ValueOrZero()) if err := orgTypeEnum.ValidateEnum(tx); err != nil { return err } return nil }
排查建议
- 验证PostgreSQL套接字路径:执行
ls /tmp/.s.PGSQL.5432,确认套接字文件存在。如果不存在,修改PostgreSQL配置文件postgresql.conf中的unix_socket_directories为/tmp,或者将环境变量DB_HOSTNAME改为实际套接字目录。 - 直接测试数据库连接:用psql命令测试连接:
psql -h /tmp -p 5432 -U solomonng aac_development,根据返回的错误信息定位问题(比如数据库不存在、用户权限不足等)。 - 检查数据库初始化代码:你提供的代码仅包含模型定义,需确认服务启动时的数据库连接逻辑是否正确读取环境变量,构建的GORM DSN是否符合格式(建议显式设置
sslmode=disable,因为空值可能导致连接异常)。 - 查看PostgreSQL日志:执行
brew services logs postgresql@13,查看日志中是否有连接失败的详细记录,辅助定位问题。 - 确认端口占用情况:执行
lsof -i :8000检查8000端口是否被占用,若被占用可更换端口或终止占用进程。
内容的提问来源于stack exchange,提问作者Solomon Ng
相关产品推荐
相关产品推荐

