Nginx + uwsgi + django + odbc - issues with not finding odbc driver, possibly related to wrong user somewhere
<p>I'm trying to connect to teradata through odbc from django under CentOS. The problem is that odbc cannot find teradata driver when run under django. If I run the script from python directly (or through django's <code>./manage command</code>) it works fine, which makes me to believe that something in the chain above is being run under wrong (possibly "nginx") user, as I assume teradata (odbc?) is unable to locate <code>.odbc.ini</code> in the home directory of the current user. </p>

<p>I had similar problem under Debian and solved it by changing uwsgi user to match, though CentOS uwsgi config is slightly different and the same change doesn't help.</p>

<p>The error I'm getting:</p>

<pre><code>ERROR - ('DRIVER_NOT_FOUND', "No driver found for 'Teradata'.  Available drivers: PostgreSQL,MySQL")
</code></pre>

<p><code>.odbc.ini</code> with teradata config is located under /home/myuser/.odbc.ini</p>

<blockquote>
  <p>/etc/systemd/system/uwsgi.service</p>
</blockquote>

<pre><code>[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown myuser:myuser /run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites --logto /home/myuser/log.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target
</code></pre>

<blockquote>
  <p>uwsgi app.ini config</p>
</blockquote>

<pre><code>[uwsgi]
chmod-socket = 664
chown-socket = nginx #cannot change this to myuser, getting 502
uid = myuser #this is what fixed it on Debian
gid = myuser #this is what fixed it on Debian
vhost = true
plugins = python
socket = /home/myuser/app.sock
master = true
enable-threads = true
processes = 2
module = app.wsgi
wsgi-file = /home/myuser/wsgi.py
chdir = /home/myuser/app/
post-buffering = 1
buffer-size = 32768
vacuum = false
</code></pre>

<blockquote>
  <p>nginx config</p>
</blockquote>

<pre><code>upstream django {
    server unix:/home/myuser/app.sock;
}

server {
    listen      80;
    server_name myapp.com;

    location / {
        uwsgi_pass  django;
        include     /home/myuser/uwsgi_params;
    }
}
</code></pre>

<blockquote>
  <p>teradata connection log (seeing correct user here too)</p>
</blockquote>

<pre><code>/********************************************************************************
 * Application Name: myapp
 *          Version: 1.0
 *       Run Number: xxx
 *             Host: pushnotif01
 *         Platform: Linux-centos
 *          OS User: myuser
 *   Python Version: 2.7.5
 *  Python Compiler: GCC 4.8.5 20150623 (Red Hat 4.8.5-4)
 *     Python Build: ('default', 'Sep 15 2016 22:37:39')
 *  UdaExec Version: 15.10.0.18
 *     Program Name: uwsgi
 *      Working Dir: /home/myuser
 *          Log Dir: /home/myuser/logs
 *         Log File: /home/myuser/logs/app.log
 *     Config Files: [u'/etc/udaexec.ini: Not Found', u'/home/myuser/udaexec.ini: Not Found', u'/home/myuser/app/udaexec.ini: Not Found']
 *      Query Bands: ApplicationName=myapp;Version=1.0;JobID=20161006121335-43;ClientUser=myuser;Production=False;udaAppLogFile=/home/myuser/logs/app.log;UtilityName=PyTd;UtilityVersion=15.10.0.18
********************************************************************************
</code></pre>

<blockquote>
  <p>App launch script:</p>
</blockquote>

<pre><code>sudo service nginx restart
sudo service uwsgi restart
</code></pre>

<p>If I try to create a test file from within the same code it is being created under correct <code>myuser</code> user, so it seems that django is running under the right user, yet odbc is still unable to find its config for some reason.</p>

<p>After trial and error this made it work for now:</p>

<ol>
<li><p>Add <code>HOME</code> env variable to uwsgi's app.ini config pointing to home dir of the right user:</p>

<pre><code>env=HOME=/home/myuser
</code></pre></li>
<li><p>Pass <code>odbcLibPath</code> param explicitly when initializing the teradata connection:</p>

<pre><code>udaExec = teradata.UdaExec(odbcLibPath='/opt/teradata/client/15.00/odbc_64/lib/libodbc.so', ...)
</code></pre></li>
</ol>

python django nginx teradata uwsgi