Subprocess.popen and psql

I have a Django application that needs to call psql. I do this in a celery thread that looks like this:

@task()
def insert_sqldump_threaded(username, database, file):
  host = database.server.db_address

  work = subprocess.Popen([settings.PSQL, 
                          "-f%s" % file, 
                          "-d%s" % database.db_name,
                          "-h%s" % host,
                          "-U%s" % settings.DB_ADMIN_USER
                          ], env = {'PGPASSFILE': settings.DB_PASSFILE}
                          )
  work.wait()
  return work.returncode

On my development server, PGPASSFILE looks like this:

localhost:5432:*:postgres:postgres

which should be good.

The problem is that everything I get when calling this function is a psql error:

psql: could not translate host name "localhost" to address: Unknown server error

And now it gets really weird, but when I don't send the env variable, psql seems to recognize the host. At least it asks for a password.

Any ideas on how to solve this?

+3
source share
2 answers

I think postgresql needs other environment variables that you clear when passing env. You can simply modify os.environ or make a copy of it in advance, as in the following code:

import os
@task()
def insert_sqldump_threaded(username, database, file):
  d = dict(os.environ)
  d['PGPASSFILE'] = settings.DB_PASSFILE
  host = database.server.db_address

  work = subprocess.Popen([settings.PSQL, 
                          "-f%s" % file, 
                          "-d%s" % database.db_name,
                          "-h%s" % host,
                          "-U%s" % settings.DB_ADMIN_USER
                          ], env = d
                          )
  work.wait()
  return work.returncode
+3

env, , , - . os.environ. . . os.environ.

0

Source: https://habr.com/ru/post/1792594/


All Articles