I tried to connect gorm postgres to my code (golang language), but when I run it it gives this error:
2023/11/14 07:10:15 C:/Users/Professional/Desktop/проектики голанга/filmgrpc/service/DB.go:40 [error] failed to initialize database, got error failed to connect to `host=localhost user=user database=db`: dial error (dial tcp 127.0.0.1:5432: connectex: No connection could be made because the target machine actively refused it.) 2023/11/14 07:10:15 C:/Users/Professional/Desktop/проектики голанга/filmgrpc/service/DB.go:41 failed to connect to `host=localhost user=user database=db`: dial error (dial tcp 127.0.0.1:5432: connectex: No connection could be made because the target machine actively refused it.) [0.515ms] [rows:-] SELECT count(*) FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = 'films' AND table_type = 'BASE TABLE' 2023/11/14 07:10:15 C:/Users/Professional/Desktop/проектики голанга/filmgrpc/service/DB.go:41 failed to connect to `host=localhost user=user database=db`: dial error (dial tcp 127.0.0.1:5432: connectex: No connection could be made because the target machine actively refused it.) [0.518ms] [rows:0] CREATE TABLE "films" ("id" text,"title" text,"genre" text,"created_at" timestamptz,"updated_at" timestamptz,PRIMARY KEY ("id")) 2023/11/14 07:10:15 Error connecting to the database...failed to connect to `host=localhost user=user database=db`: dial error (dial tcp 127.0.0.1:5432: connectex: No connection could be made because the target machine actively refused it.) exit status 1
package main
import (
"fmt"
"log"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func init() {
DatabaseConnection()
}
var DB *gorm.DB
var err error
type Film struct {
ID string `gorm:"primarykey"`
Title string
Genre string
CreatedAt time.Time `gorm:"autoCreateTime:false"`
UpdatedAt time.Time `gorm:"autoUpdateTime:false"`
}
func DatabaseConnection() {
host := "localhost"
port := "5432"
dbname := "db"
dbuser := "user"
password := "pass"
dsn := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable",
host,
port,
dbuser,
dbname,
password,
)
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
DB.AutoMigrate(Film{})
if err != nil {
log.Fatal("Error connecting to the database...", err)
}
fmt.Println("Database connection successful...")
}
I also thought that I should try it with Docker
(here is the command docker run -it --name film-frnxx -e POSTGRES_PASSWORD=password -e POSTGRES_USER=dbuser -e POSTGRES_DB=dbname -e POSTGRES_PORT=port postgres).
The code itself seems to be correct.
It suggests that there is no PostgreSQL server running on the specified host and port, or the server is actively refusing the connection.
1-Check PostgreSQL Server: Ensure that PostgreSQL is installed on your machine, and the server is running. You can use the following command to start the PostgreSQL server:
Make sure to replace service with the appropriate command for your operating system.
2-Check Firewall Settings
3-Check PostgreSQL Authentication: Ensure that the PostgreSQL server is configured to allow connections from the specified user (dbuser) and that the password is correct. You might need to check the pg_hba.conf file for authentication settings.
4-Use gorm.Open at Runtime: Consider moving the gorm.Open call from the init function to the main function to ensure the connection attempt occurs when your application is running.