For the sake of completeness: SQL-standard compliant databases have table information_schema_catalog_name, which always contains one row and one column containing the name of the current database (the current 'catalog', in SQL terminology).

(The SQL standard says "The value of CATALOG_NAME is the name of the catalog in which this Information Schema resides.")

PostgreSQL example:

testdb=# select CATALOG_NAME from INFORMATION_SCHEMA.INFORMATION_SCHEM +A_CATALOG_NAME; catalog_name -------------- testdb (1 row)

I'd normally prefer DBI, but the SQL can be handy.

(And FWIW: postgres also supports the more succinct and standard-compliant CURRENT_CATALOG:)

testdb=# select current_catalog; current_database ------------------ testdb (1 row)

And then there is DBI's get_info stuff:

use DBI::Const::GetInfoType; say "SQL_DBMS_NAME [",$dbh->get_info($GetInfoType{SQL_DBMS_NAME + }),"]"; say "SQL_DBMS_VER [",$dbh->get_info($GetInfoType{SQL_DBMS_VER + }),"]"; say "SQL_DATABASE_NAME [",$dbh->get_info($GetInfoType{SQL_DATABASE_NAM +E}),"]"; say "SQL_SERVER_NAME [",$dbh->get_info($GetInfoType{SQL_SERVER_NAME + }),"]"; -- output SQL_DBMS_NAME [PostgreSQL] SQL_DBMS_VER [09.02.0000] SQL_DATABASE_NAME [testdb] SQL_SERVER_NAME [testdb]

update: Jenda: Re SQL-standard versions: PostgreSQL supports generally quite recent versions: SQL:2011 and SQL:2008. And even SQL92 already prescribed INFORMATION_SCHEMA_CATALOG_NAME.

Some systems are more SQL-compliant than others. PostgreSQL tries quite hard to implement as much as possible.


In reply to Re: DBI:how to get name of the db ? by erix
in thread DBI:how to get name of the db ? by ansh batra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.