Setting Database Name During Connection
2015-05-20 17:59:46 GMT
The common way to control which database you connect to is apparently through the DSN, or connection string. However, ODBC also allows the database name (distinct from which host/port) to be specified programmatically, but it appears that this may be unimplemented in psqlODBC.
Calling SQLSetConnectAttr with the SQL_ATTR_CURRENT_CATALOG attribute should allow you to specify which database. My expectation is that doing so should override any value specified in the DSN or connection string.
In practice, the SQLSetConnectAttr call returns SQL_SUCCESS, regardless of what database ("catalog") name you specify, and regardless of whether you call it prior to SQLConnect or after. However, calling SQLGetConnectAttr afterwards reveals that nothing was changed by the Set. Attempting to use the specified database fails, consistent with the current database still being whatever was specified in the DSN.
I downloaded the current snapshot (psqlodbc-355ac88) and grepped around, but did not find any references to SQL_ATTR_CURRENT_CATALOG, nor to 3D000 which is the error code for an "Invalid catalog name", which leads me to think it may just be unimplemented and not a more subtle bug. I did not do a more serious investigation however, so take that with a large grain of salt.
Tested on Windows 7, using the odbc driver "Postgre SQL ANSI(x64) 9.03.03.00" with PostgreSQL 9.2. Testing using the same code works as expected for SQL Server.
As an aside from the bug report, is it correct that there is NOT a way to change the current database through a SQL command with Postgres (i.e. there is no equivalent to "USE" in SQL Server)?