3

actually i have googled a bit and i need corresponding SELECT command to following PostgreSQL shell command :

\dt schemaname.*

i managed to get all databases with following code :

            Statement statement = (Statement) connection.createStatement();
            ResultSet rs = statement
                    .executeQuery("SELECT datname FROM pg_database");
            while (rs.next()) {
                System.out.println("DB Name : " + rs.getString(1));
           //i need another while here to list tables 
           //inside the selected database
}

i tried following statement, but no luck :

statement.executeQuery("SELECT table_schema,table_name FROM "
                                + rs.getString(1)
                                + " ORDER BY table_schema,table_name");

this is the error i am getting :

org.postgresql.util.PSQLException: ERROR: relation "template1" does not exist
  Position: 37
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
    at com.isiran.rayten.rg.db.bare.wrapper.PGWrap.main(PGWrap.java:64)
2
  • the connection string is wrong, you are pointing into template1 database. Commented Oct 28, 2013 at 10:08
  • You are apparently using the database name as the table name in your second executeQuery() Commented Oct 28, 2013 at 10:09

3 Answers 3

8

Use the DatabaseMetaData object to query information, eg getTables(...):

DatabaseMetaData dbmd = connection.getMetaData();
try (ResultSet tables = dbmd.getTables(null, null, "%", new String[] { "TABLE" })) {
    while (tables.next()) {
        System.out.println(tables.getString("TABLE_NAME"));
    }
}

This will return all tables in the database, you may need to specify values for catalog and/or schemaPattern to get a more specific result.

Sign up to request clarification or add additional context in comments.

3 Comments

If you follow the link to the javadoc, you can see that schemaPattern is the second parameter.
it is working but the problem is it cant load db names so i cant pass the name as argument
As far as I know getSchemas() is implemented for PostgreSQL
7

If you use psql -E, it'll echo the actual queries that get run when you type commands such as \dt:

denis=# \dt public.*
********* QUERY **********
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','s','')
      AND n.nspname !~ '^pg_toast'
  AND n.nspname ~ '^(public)$'
ORDER BY 1,2;
**************************

These queries can then be simplified or amended per your specific use-case.

1 Comment

+1 for helpful comment, but as Nicolai mentioned i have to connect to schema first, i am not sure how to do that within java since it is not a query
4

To list all tables from database you have to read table pg_catalog.pg_tables But unfortunately you have to be logged-in in database. So in place where you wrote comments

//i need another while here to list tables 
//inside the selected database

Before loop for tables you need to log-in in this database.

4 Comments

and how can i do that? i know i can do it by typing connect schema in shell but not sure how to do that it java
In the same way as object @connection@ was initialized in your code, but with different database name. But you need user name and password for each database or connect to databases with superuser user (e.g. postgres).
hmm. i have already connected to that database, with jdbc connection string :-?
Yes, but you try to list tables from other databases. You can't do this from current database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.