Postgres – Golang 错误:pq: 用户不存在

huangapple go评论82阅读模式
英文:

Postgres - Golang Error: pq: Users does not exists

问题

我正在尝试将Golang连接到一个位于Gcloud上的Postgres数据库,并且我已经允许了网络IP。我正在尝试访问User数据库,然后是public模式下的User表,这是数据库的结构:

我遇到了这个错误:pq: database "Users" does not exist

我将添加代码:

package ports

import (
	"database/sql"
	"log"

	"github.com/gofiber/fiber/v2"
	_ "github.com/lib/pq"
)

func OpenConnection(dbName string) (*sql.DB, error) {
	connStr := "<addr>" + dbName
	db, err := sql.Open("postgres", connStr)

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

	return db, err
}

func GetUsers(ctx *fiber.Ctx) error {
	db, dbErr := OpenConnection("Users")

	if dbErr != nil {
		log.Fatalln(dbErr)
	}

	defer db.Close()
	rows, err := db.Query("SELECT * FROM Users")

	if err != nil {
		log.Fatalln(err)
		ctx.JSON("An error ocurred")
	}

	defer rows.Close()

	return ctx.Render("/users", fiber.Map{
		"Users": rows,
	})
}

Postgres – Golang 错误:pq: 用户不存在

英文:

I'm trying to connect Golang to a Postgres DB, it is in Gcloud and I already allowed the network IP. I'm trying to access the User DB and then the User table inside the public schema, this is the database structure:

Postgres – Golang 错误:pq: 用户不存在

I am getting this error: pq: database &quot;Users&quot; does not exist

I'll add the code:

package ports

import (
	&quot;database/sql&quot;
	&quot;log&quot;

	&quot;github.com/gofiber/fiber/v2&quot;
	_ &quot;github.com/lib/pq&quot;
)

func OpenConnection(dbName string) (*sql.DB, error) {
	connStr := &quot;&lt;addr&gt;&quot; + dbName
	db, err := sql.Open(&quot;postgres&quot;, connStr)

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

	return db, err
}

func GetUsers(ctx *fiber.Ctx) error {
	db, dbErr := OpenConnection(&quot;Users&quot;)

	if dbErr != nil {
		log.Fatalln(dbErr)
	}

	defer db.Close()
	rows, err := db.Query(&quot;SELECT * FROM Users&quot;)

	if err != nil {
		log.Fatalln(err)
		ctx.JSON(&quot;An error ocurred&quot;)
	}

	defer rows.Close()

	return ctx.Render(&quot;/users&quot;, fiber.Map{
		&quot;Users&quot;: rows,
	})
}

答案1

得分: 0

我刚刚修复了它,问题是两个方面的组合:

  1. 当我硬编码时,我没有正确地组合字符串,这样就解决了数据库错误。
  2. 之后,我遇到了一个错误,它会说表不存在,问题是我发送了一个未引用的表名,我需要它区分大小写,所以我必须用引号将名称包裹起来使其工作。像这样:
package ports

import (
	"database/sql"
	"log"

	"github.com/gofiber/fiber/v2"
	_ "github.com/lib/pq"
)

func OpenConnection() (*sql.DB, error) {
	connStr := "<connStr>/users"
	db, err := sql.Open("postgres", connStr)

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

	return db, err
}

func GetUsers(ctx *fiber.Ctx, db *sql.DB) error {

	defer db.Close()
	rows, err := db.Query(`SELECT * FROM "Users"`)

	if err != nil {
		log.Fatalln(err)
		ctx.JSON("An error ocurred")
	}

	defer rows.Close()

	return ctx.Render("/users", fiber.Map{
		"Users": rows,
	})
}
英文:

I just fixed it and it was a combination of two things:

  1. I wasn't properly combining the strings, when I hardcoded it I got rid of the db error.
  2. After that I encountered an error where it would say the table doesn't exists, the issue is I was sending an unquote table name and I needed it to be case sensitive so I had to wrap the name between quotes to make it work. Like so
package ports

import (
	&quot;database/sql&quot;
	&quot;log&quot;

	&quot;github.com/gofiber/fiber/v2&quot;
	_ &quot;github.com/lib/pq&quot;
)

func OpenConnection() (*sql.DB, error) {
	connStr := &quot;&lt;connStr&gt;/users&quot;
	db, err := sql.Open(&quot;postgres&quot;, connStr)

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

	return db, err
}

func GetUsers(ctx *fiber.Ctx, db *sql.DB) error {

	defer db.Close()
	rows, err := db.Query(`SELECT * FROM &quot;Users&quot;`)

	if err != nil {
		log.Fatalln(err)
		ctx.JSON(&quot;An error ocurred&quot;)
	}

	defer rows.Close()

	return ctx.Render(&quot;/users&quot;, fiber.Map{
		&quot;Users&quot;: rows,
	})
}

huangapple
  • 本文由 发表于 2022年3月31日 23:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71695502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定