Question

Connection made to postgre server but datas are not fetching from table "students" of "student" database

package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	_ "github.com/lib/pq"
)

func logFatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

const (
	HOST     = "localhost"
	PORT     = 5432
	USER     = "Oluwashola"
	PASSWORD = ""
	DBNAME   = "student"
)

type student struct {
	ID          int    `json:"id"`
	Name        string `json:"name"`
	Department  string `json:"department"`
	Year_Joined string `json:"year_joined"`
}

var students []student

var db *sql.DB

func main() {

	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		HOST, PORT, USER, PASSWORD, DBNAME)

	var err error

	db, err = sql.Open("postgres", psqlInfo)
	logFatal(err)

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Connected")

	router := mux.NewRouter()
	router.HandleFunc("/students", getStudents).Methods("GET")

	log.Fatal(http.ListenAndServe(":8000", router))

}

func getStudents(w http.ResponseWriter, r *http.Request) {
	var theStudent student

	students = []student{}

	rows, err := db.Query("select * from students")

	if err != nil {
		fmt.Print("there was an error", err)
	}

	for rows.Next() {
		err := rows.Scan(&theStudent.ID, &theStudent.Name, &theStudent.Department, &theStudent.Year_Joined)

		if err != nil {
			log.Fatal(err)
		}
		students = append(students, theStudent)

	}

	json.NewEncoder(w).Encode(students)

}

And this is the result am getting from vscode terminal

Connected

there was an error pq: relation "students" does not exist<nil>`enter code here

And this is my database

Last login: Sat May 29 07:24:12 on ttys003
/Applications/Postgres.app/Contents/Versions/11/bin/psql -p5432 "student"
 ~   new-feature ●✚  /Applications/Postgres.app/Contents/Versions/11/bin/psql -p5432 "student"
psql (11.2)
Type "help" for help.

student=# select * from students;
 id | name  | department | year_joined 
----+-------+------------+-------------
  1 | ola   | csc        | 1990
  2 | Kenny | math       | 2019
(2 rows)

student=# 

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there,

Are you running this inside a Docker container? If so you might have to use the Docker host network to be able to connect to the correct PostgreSQL database.

Regards, Bobby