Thursday, May 2, 2024
HomeGolangCan not hook up with database: failed to connect with `host=localhost person=root...

Can not hook up with database: failed to connect with `host=localhost person=root database=userdb`: didn’t obtain message (sudden EOF – Code Assessment


essential.go

package deal essential

import (
	"fmt"
	"log"
	"internet/http"
	"os"

	"github.com/jmechavez/PSO-Sarangani/db"
)

kind Config struct {
	Port string
}

kind Utility struct {
	Config Config
}

// international port varialbe for each essential and serve
var port = os.Getenv("PORT")

func (app *Utility) Serve() error {
	fmt.Println("API listening on port", port)

	srv := &http.Server{
		Addr: fmt.Sprintf(":%s", port),
	}

	return srv.ListenAndServe()
}

db.go


package deal db

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/jackc/pgconn"
	_ "github.com/jackc/pgx/v4"
	_ "github.com/jackc/pgx/v4/stdlib"
	_ "github.com/lib/pq"
)

kind DB struct {
	DB *sql.DB
}

var dbConn = &DB{}

const (
	maxOpenDbConn = 10
	maxIdleDbConn = 5
	maxDbLifeTime = 5 * time.Minute
)

func ConnectPostgres(dsn string) (*DB, error) {
	d, err := sql.Open("pgx", dsn)
	if err != nil {
		return nil, err
	}
	d.SetMaxOpenConns(maxOpenDbConn)
	d.SetMaxIdleConns(maxIdleDbConn)
	d.SetConnMaxLifetime(maxDbLifeTime)

	err = testDB(d)
	if err != nil {
		return nil, err
	}
	dbConn.DB = d
	return dbConn, nil
}

func testDB(d *sql.DB) error {
	err := d.Ping()
	if err != nil {
		fmt.Println("Error", err)
		return err
	}
	fmt.Println("*** Pinged database efficiently! ***")
	return nil
}

Makefile


DSN="host=localhost port=5432 person=root password=secret dbname=userdb sslmode=disable timezone=UTC connect_timeout=5"
PORT=8080
DB_DOCKER_CONTAINER=psosarangani_db
BINARY_NAME=psosaranganiapi

# ! creating the container with postgres software program
postgres:
	docker run --name ${DB_DOCKER_CONTAINER} -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:12-alpine


# ! creating the espresso db contained in the postgres container
createdb:
	docker exec -it ${DB_DOCKER_CONTAINER} createdb --username=root --owner=root userdb 

#	docker exec -it $(DB_DOCKER_CONTAINER) psql --username=root --command "CREATE DATABASE userdb OWNER root;"


# ! cease different docker containers
stop_containers:
	echo "Stopping different docker containers"
	if [ $$(docker ps -q) ]; then 
		echo "Discovered and stopped docker containers..."; 
		docker cease $$(docker ps -q); 
	else 
		echo "No lively containers discovered..."; 
	fi


# ! begin docker container 
start-docker:
	docker begin ${DB_DOCKER_CONTAINER}

create_migrations:
	sqlx migrate add -r init

migrate-up:
	sqlx migrate run --database-url "postgres://root:secret@localhost:5432/userdb?sslmode=disable"

migrate-down:
	sqlx migrate revert --database-url "postgres://root:secret@localhost:5432/userdb?sslmode=disable"

construct:
	@echo "Constructing backend api binary"
	go construct -o ${BINARY_NAME} cmd/server/*.go
	@echo "Binary constructed!"

run: construct stop_containers start-docker
	@echo "Startin api"
	@env PORT=${PORT} DSN=${DSN} ./${BINARY_NAME} &
	@echo "api began!"

cease:
	@echo "Stopping backend"
	@-pkill -SIGTERM -f "./${BINARY_NAME}"
	@echo "Stopped backend"

begin: run

restart: cease begin

sorry i’m strive my finest to resolve this… please assist me. simply finding out golang and postgre

Hello @John_Michael_Echavez, welcome to the discussion board.

On a primary look, you would possibly want to reveal the container to the host’s community by including --network host to your docker run command.

thanks man it work… i see now what’s the drawback.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments