go-vue-starter icon indicating copy to clipboard operation
go-vue-starter copied to clipboard

sql: database is closed

Open goodforever opened this issue 7 years ago • 2 comments

I use postgresql server ,When I go run server.go ,I got

(sql: database is closed) [2018-07-20 11:36:04]

(sql: database is closed) [2018-07-20 11:36:04]

(sql: database is closed) [2018-07-20 11:36:04]

(sql: database is closed) [2018-07-20 11:36:04]
[negroni] listening on :3000

goodforever avatar Jul 20 '18 03:07 goodforever

I have not tried Postgres, I would check your config info.

markcheno avatar Jul 27 '18 21:07 markcheno

@goodforever I double-checked and I was able to get Postgres working.

Changed server.go to have

db := models.NewPostgresDB("postgres://postgres:password@localhost/pqgotest?sslmode=disable")

Instead of

db := models.NewSqliteDB("data.db")

Make sure that you change any credentials and database accordingly. Also, I believe you will have to create the database prior to starting the server. Unlike Sqlite, Postgres can have multiple databases under the same connection (sqlite would require multiple files as far as I know, maybe I am wrong), which means the driver needs the database to exist to connect to it. If you have done all that I am not sure why it wouldn't be working but like I said I verifed it to working.

FYI: I used docker for both the database and database management tools

docker run --name go-postgres -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres

docker pull dpage/pgadmin4

docker run -p 80:80 \
-e "[email protected]" \
-e "PGADMIN_DEFAULT_PASSWORD=password" \
--link go-postgres:go-postgres -d dpage/pgadmin4

When the database was not created I did get an error on startup that was different then yours

panic: pq: database "pqgotest" does not exist

goroutine 1 [running]:
github.com/markcheno/go-vue-starter/models.NewPostgresDB(0x45d2f1f, 0x3f, 0x0)
	/Users/******/go/src/github.com/markcheno/go-vue-starter/models/db.go:21 +0x172
main.main()
	/Users/******/go/src/github.com/markcheno/go-vue-starter/server.go:11 +0x3a
exit status 2

catdevman avatar Sep 16 '18 04:09 catdevman