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

package main

import (

	_ ""

func logFatal(err error) {
	if err != nil {

const (
	HOST     = "localhost"
	PORT     = 5432
	USER     = "Oluwashola"
	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",

	var err error

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

	err = db.Ping()
	if err != nil {


	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 {
		students = append(students, theStudent)




And this is the result am getting from vscode terminal


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/ -p5432 "student"
 ~   new-feature ●✚  /Applications/ -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)


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