lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.01k stars 910 forks source link

Unable to list the tables #958

Closed VineethReddy02 closed 4 years ago

VineethReddy02 commented 4 years ago

Usually in postgres. We use SHOW TABLES; to list the tables. Can I know how can I list using pq client?

r, err := c.Session.Query(fmt.Sprintf( "SHOW TABLES;"))

I see an error message saying:

unrecognized configuration parameter "tables"

peterstace commented 4 years ago

You could use the information_schema.tables table, which contains metadata about each table.

VineethReddy02 commented 4 years ago

@peterstace Is there any way I can list all the tables. something like /dt & SHOW TABLES;

k, err := c.Session.Exec(fmt.Sprintf(` CREATE TABLE %s ( hash VARCHAR (50) PRIMARY KEY, range VARCHAR (50) NOT NULL, value VARCHAR (50) NOT NULL );`, "a4"))

I need a query like this for listing all the tables. But I couldn't understand. How I can do so and I stuck here.

I would appreciate the help here.

cbandy commented 4 years ago

PostgreSQL doesn't have a SHOW TABLES: https://www.postgresql.org/docs/current/sql-commands.html

You can use the ECHO_HIDDEN variable in psql to show the queries that drive \dt and other commands: https://www.postgresql.org/docs/current/app-psql.html

psql> \set ECHO_HIDDEN on
psql> \dt
********* QUERY **********
SELECT n.nspname as "Schema",
…