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

sql: unknown driver "postgres" #51

Closed
pilinux opened this issue Jun 17, 2021 · 4 comments
Closed

sql: unknown driver "postgres" #51

pilinux opened this issue Jun 17, 2021 · 4 comments
Assignees

Comments

@pilinux
Copy link

pilinux commented Jun 17, 2021

Your Question

import (
  "database/sql"

  "gorm.io/gorm"
  "gorm.io/gorm/logger"
  "gorm.io/driver/postgres"
)

func abc() {
  ... ... ...
  sqlDB, err = sql.Open("postgres", dsn)

  db, err = gorm.Open(postgres.New(postgres.Config{
    Conn: sqlDB,
     }), &gorm.Config{})
... ... ...
}

It fails to establish a connection with the PostgreSQL database.

Error message:
sql: unknown driver "postgres" (forgotten import?)

The document you expected this should be explained

official documents site

Expected answer

With MySQL and SQLite3 drivers gorm.io/driver/mysql, gorm.io/driver/sqlite, everything works perfectly fine.

Temporary workaround

By importing _ "github.com/jinzhu/gorm/dialects/postgres" along with "gorm.io/driver/postgres", connection with PostgreSQL database works smoothly.

So, why isn't "gorm.io/driver/postgres" works without importing _ "github.com/jinzhu/gorm/dialects/postgres" ? Am I missing something?

Thank you in advance.

@sebastianbuechler
Copy link

Why are you using database/sql? For me it works when calling directly db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

@pilinux
Copy link
Author

pilinux commented Jul 31, 2021

Why are you using database/sql? For me it works when calling directly db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

https://pkg.go.dev/gorm.io/driver/postgres@v1.1.0?utm_source=gopls#Config

type Config struct {
	DriverName           string
	DSN                  string
	PreferSimpleProtocol bool
	WithoutReturning     bool
	Conn                 *sql.DB
}

For custom configurations (example: max number of connections in the idle connection pool, max number of open connections in the database, etc.), I am using database/sql.

@sebastianbuechler
Copy link

Did you try to use the gorm implementation of these features as seen here? https://gorm.io/docs/connecting_to_the_database.html#Connection-Pool

@pilinux
Copy link
Author

pilinux commented Jul 31, 2021

Did you try to use the gorm implementation of these features as seen here? https://gorm.io/docs/connecting_to_the_database.html#Connection-Pool

Yes exactly. You can see the full code here (facing issue only with postgres):
https://github.com/piLinux/GoREST/blob/6f54b916bc3eda95201ac5c0b456e1838693095d/database/dbConnect.go

@jinzhu jinzhu closed this as completed Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants