Query to list the all database in db2 - python-3.x

"SELECT name FROM master.sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')"
In SQL server this above command used to retrieve the database list.
similarly, what is the command to retrieve the database list in DB2 using python program?
db2 server platform : windows
database : catalogued

With Db2-LUW, you do not use SQL to detect which databases are available, instead that is done via commands, although other methods are available.
For Db2-LUW with catalogued databases, you can use python to read the database directory by parsing the output of the command db2 list db directory assuming that you have a Db2-client which includes the db2 command line processor.
Note that non-IBM (third party) Db2-clients might have other functionality, so this answer only concerns Db2 clients supplied from IBM that include the Db2 CLP program.
You will need to understand the legacy concepts of catalogued databases , all of which are explained in the free online Db2 Knowledge Centre. See commands (non-SQL) list database directory and catalog database and catalog tcpip node etc.
If your databases are not catalogued in the database directory, but instead are configured in XML file called db2dsdriver.cfg or equivalent, then you cannot use this method, although you can parse the XML file looking for DATABASE and DSN stanzas.
Additionally, if your databases are on i-series , then different considerations apply for IBM i access databases.
There is a sample code here, in the function query_sdb_dir (you can parse the output of the command db2 list db directory ). BUT this assumes that you have a Db2 client installed that includes the Db2 CLP (i.e on MS-Windows, that you have db2.exe on the PATH, this is the command line processor CLP). By default with Python ibm_db you do not get this executable because it is not part of the tiny footprint CLIDRIVER that comes by default with the python ibm_db module. But you can first install a Db2-driver that includes the CLP (db2.exe on MS-Windows), set environment variable IBM_DB_HOME to point to its installation location, then pip install ibm_db and it will use your pre-existing Db2-driver and will not install the default clidriver.
There are other ways, typically that involve a database connection and using the monitoring functionality. But the discovery action (without a database connection) seems to be the topic of your question.

The following statement (if you have permissions to use these table functions) returns a list of active databases managed by the same Db2 for LUW instance, which manages the database you are connected to.
-- Preferred method since Db2 9.7
SELECT DISTINCT DB_NAME
FROM TABLE (MON_GET_MEMORY_SET ('DATABASE', NULL, -2))
-- For Db2 versions before 9.7
SELECT DISTINCT DB_NAME
FROM TABLE (SNAP_GET_DB (NULL))
You won't see non-active databases (if any) in this list.

Related

Need overview explanation of dbase connectivity using python

I have been working with python and postgreql for over a year. I can connect and work with postgres databases by blindly using various libraries. But whenever I change platform (most recently from macOS laptop to remote ubuntu server) I go through a day or so of trying to get libraries working eg. I was using 'pyodbc' in some modules but when I migrated the code to the server I had to switch to 'pg8000' because the modules as they were kept throwing errors.
Can someone explain or point me to a link explaining how python connects to dB's? For example, why do I need a MS ODBC driver for 'pyodbc' to connect to an Azure SQL or postgresql but 'pg8000' seems to need nothing at all to connect to a postgresql? When I move to an Ubuntu environment and install ODBC drivers they show up on root under /etc, and /opt (for MS ODBC) but also in my Conda environment (/anaconda3/envs/) and I don't know which is the correct choice for 'ODBC.ini'?
Like I say, I can get things working but really have no understanding as to why they are working and that means I waste time experimenting every time I deal with a change in environment. I've not yet found an explanation online that covers more than a very specific circumstance eg. 'here's how to install our driver ...' Any help would be appreciated.
Final Update:
Following the responses esp. #Thompson the diagram below seems to be the final interpretation and I have a better idea of where to look for answers. For the record pyodbc, SQLAlchemy and pg8000 have been my tools of choice with no problems except as described in the question.
pyodbc is not actually a driver and doesn't contain one, its a 'module for ODBC databases', so it's more of an interface from python to ODBC driver to some database. That's why to use it you have to have an actual separate driver to connect to. Azure SQL being owned by Microsoft would reasonably require Microsoft's ODBC driver, while Postgres will require a Postgres ODBC driver, etc...
The ODBC driver manager is platform-specific, while the ODBC driver is database-specific. That would explain why if you are you are changing platforms or databases, you need to change drivers.
As Adrian noted, you don't need ODBC drivers for postgres, it is more common to use postgres/python drivers (eg: https://wiki.postgresql.org/wiki/Python)
psycopg2 is an actual PostgresSQL driver. It serves as client from Python to postgres, no intermediary required. That's why you don't need to install anything else when you use it. I haven't used pg8000, but based on this list it's a driver too, so you wan't need anything else.
EDITED TO ADD:
Think of a database as some 'black box' you need to activate, and its drivers as electrical sockets. ODBC driver is a specific type of socket (ODBC is a standard developed by Microsoft). If you are using ODBC plug from python (like pyodbc) to a database, you need to make sure the database has an ODBC socket installed/activated.
But your database can have other sockets too, like python-compatible DBAPI that's available on postgres. In that case you use a different direct DBAPI connector, like psycopg2.
Drivers are specific to a database. ODBC is a two stage process. There is the ODBC driver manager and then there are the database specific drivers that allow you to talk to a database. You don't need ODBC to connect to a Postgresql server. If you are going through Python you just need one of the Postgres drivers. You have already found pg8000. My preference is psycopg2.

ODBC driver connection string error

This is a general question that I can't seem to find any specific answer to. I am using a node module: https://www.npmjs.com/package/odbc
The module says to install a ODBC driver for the specified database and also have a properly configured odbc.ini and odbcinst.ini.
I am also using docker to run my node script.
My question is on my docker container, do I install each driver for the database? And then on the database server I have to configure the odbc.ini? Or on my node server I set this up? Or does it already come pre configured on some databases?
I keep getting the error: "Data source name not found, and no default driver specified"
Which I am guessing since I probably don't have the driver setup in my node container and the odbc.ini properly setup on my database server.
I am currently testing on a mysql database but want to be able to connect to any ODBC database.
Here is a link to setup mysql on ubuntu:
http://www.kaffeetalk.de/how-to-setup-and-configure-mysql-with-unixodbc-under-ubuntu-14-04/
odbc.ini contains DSN (Data Source Name) definitions, which are based on drivers registered in the odbcinst.ini, both found along with the drivers themselves on the ODBC data consuming host/environment (here, your docker container). These configuration files are typically (and optimally) respectively targeted by $ODBCINI and $ODBCINSTINI environment variables, but sometimes found at default locations like /etc/odbc.ini or ~/.odbc.ini.
You might look at some documentation, like the ODBC spec on MSDN, or developer guidance on iODBC.org. Do be sure to use ODBC's DBMS-agnostic SQL syntax for your queries, and not the DBMS-specific dialect for MySQL (or any other DBMS)!

Assigning DB2 databases to 32-bit ODBC on a 64-bit machine via command line

I'm attempting to connect to a DB2 database through Excel (From Other sources > From Microsoft Query) on a 64-bit machine. On a 32-bit machine, this has been as simple as just opening db2cmd and running DB2 -f databases.txt (which contains a list of our databases catalogs them, generally via 'Catalog database Foo as Bar at node Foobar', then 'Catalog system odbc data source Bar') and everything is correctly in the list.
However, on the 64-bit machine, Excel appears to be checking the 32-bit ODBC connections, which does not contain the databases. Is there a way I need to catalog the databases in order to populate them into the 32-bit ODBC list? I can go in and assign them by hand, but the entire point here is to automate the process, so a command line solution is preferred.

Copy / Replace DB2 Table Data from one Remote DB Server to Another

I have two IBM DB2 servers at separate remote locations.
I need to copy data from several tables on one database on the first server to another database in the second server.
Previously I've used the IBM data studio tool to export the data and Load Replace it into the other table in the second server.
I am in need of a way to automate this. Probably through command line shell scripts.
How can I accomplish this?
You can always use the "data movement" tools included in DB2. Using EXPORT in one side (source server), and then IMPORT or LOAD in the other (target server). However, you have to take care of the transportation. I mean, you have to copy the data from one server to the other (via SCP, FTP, etc) and you can automate all of that.
Also, you can use a new tool called INGEST. It is a client tool, and this tool will put the data in the target tables of the remote server. This means that the source server will be the client of the remote server (you can catalog a remote database, in a database server).
Finally, you can create a federation between the two server (and this is my favorite). This means that in a database (for example in the target server) you present the tables of the other (source) server. This allows you to do queries between local and remote tables, and this will also allow you to create a LOAD to a local table from a CURSOR that references a SELECT of a table in the remote server.
The last option could sound complicated but it is not. You just have to define remote elements (nickname, wrapper, etc) and that is it. Once, you have configure that, you do not have to worry about file transfer, states, etc. This option is free because both server are DB2 (you can do that with other RDBS)
For more information:
Ingest: http://publib.boulder.ibm.com/infocenter/db2luw/v10r1/topic/com.ibm.db2.luw.admin.dm.doc/doc/c0057237.html
Load, Import and Export and at the same level of the previous link (Database Administration > Data movement utilities and reference)
There are multiple options (using scripting or DB2 replication):
Script to export data on one server, eventually tar/gz and send it to the 2nd server per SSH/SCP. On the 2nd Server another script extracts and loads the data.
DB2 hat build in replication support.
Check DB2 infocenter#IBM or Google. This is an easy and very common task.

How to find the connection information to Sybase DB

I have Sybase installed on my Linux box (to which I have "root" access). However I do not have the access details to Sybase, is there any way I can find the port Sybase is listening on, and also use linux root user to connect to the database.
You should check sybase interface file at the first.
Read f.e. this article about interface file structure.
http://www.outlands.demon.co.uk/sybase/index.html
After that copy env. variables from sybase user bash profile ant try to connect to database with isql utility (also explained in this article).

Resources