cli: supabase db diff fails with: psycopg2.OperationalError: could not translate host name "localhost" to address: Try again

Describe the bug I’m trying to do supabase db diff but it fails with psycopg2.OperationalError: could not translate host name "localhost" to address: Try again. I even tried changing the DNS resolver from native to https, but it doesn’t work

To Reproduce Steps to reproduce the behavior:

  1. Try to do a supabase db diff

Expected behavior Normal diff to get written

Screenshots

Logs:

$ supabase db diff --dns-resolver https
Connecting to local database...
Creating shadow database...
Setting up initial schema...
Applying migration 20220822165112_xyz.sql...
Applying migration 20220822165112_xyo.sql...
Diffing schemas: extensions,public
error diffing schema: error running container: exit 1:                                                                                                 
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1.1; however, version 23.2.1 is available.                                                                       
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.                                                    
Traceback (most recent call last):                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/schemainspect/misc.py", line 9, in connection_from_s_or_c                                               
    s_or_c.engine                                                                                                                                      
AttributeError: 'Session' object has no attribute 'engine'                                                                                             
                                                                                                                                                       
During handling of the above exception, another exception occurred:                                                                                    
                                                                                                                                                       
Traceback (most recent call last):                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect                                            
    return fn()                                                                                                                                        
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 301, in connect                                                          
    return _ConnectionFairy._checkout(self)                                                                                                            
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 761, in _checkout                                                        
    fairy = _ConnectionRecord.checkout(pool)                                                                                                           
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 419, in checkout                                                         
    rec = pool._do_get()                                                                                                                               
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 145, in _do_get                                                          
    self._dec_overflow()                                                                                                                               
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__                                                   
    compat.raise_(                                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_                                                         
    raise exception                                                                                                                                    
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 142, in _do_get                                                          
    return self._create_connection()                                                                                                                   
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 247, in _create_connection                                               
    return _ConnectionRecord(self)                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 362, in __init__                                                         
    self.__connect()                                                                                                                                   
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect                                                        
    pool.logger.debug("Error on connect(): %s", e)                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__                                                   
    compat.raise_(                                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_                                                         
    raise exception                                                                                                                                    
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 599, in __connect                                                        
    connection = pool._invoke_creator(self)                                                                                                            
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect                                                      
    return dialect.connect(*cargs, **cparams)                                                                                                          
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect                                                     
    return self.dbapi.connect(*cargs, **cparams)                                                                                                       
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect                                                             
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)                                                                             
psycopg2.OperationalError: could not translate host name "localhost" to address: Try again                                                             
                                                                                                                                                       
                                                                                                                                                       
The above exception was the direct cause of the following exception:                                                                                   
                                                                                                                                                       
Traceback (most recent call last):                                                                                                                     
  File "/usr/local/bin/migra", line 8, in <module>                                                                                                     
    sys.exit(do_command())                                                                                                                             
  File "/usr/local/lib/python3.9/site-packages/migra/command.py", line 121, in do_command                                                              
    status = run(args)                                                                                                                                 
  File "/usr/local/lib/python3.9/site-packages/migra/command.py", line 86, in run                                                                      
    m = Migration(                                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/migra/migra.py", line 31, in __init__                                                                   
    self.changes.i_from = get_inspector(                                                                                                               
  File "/usr/local/lib/python3.9/site-packages/schemainspect/get.py", line 14, in get_inspector                                                        
    c = connection_from_s_or_c(x)                                                                                                                      
  File "/usr/local/lib/python3.9/site-packages/schemainspect/misc.py", line 14, in connection_from_s_or_c                                              
    return s_or_c.connection()                                                                                                                         
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1516, in connection                                                    
    return self._connection_for_bind(                                                                                                                  
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1526, in _connection_for_bind                                          
    return self._transaction._connection_for_bind(                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 747, in _connection_for_bind                                           
    conn = bind.connect()                                                                                                                              
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3166, in connect                                                       
    return self._connection_cls(self, close_with_result=close_with_result)                                                                             
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 96, in __init__                                                        
    else engine.raw_connection()                                                                                                                       
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection                                                
    return self._wrap_pool_connect(self.pool.connect, _connection)                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3215, in _wrap_pool_connect                                            
    Connection._handle_dbapi_exception_noconnection(                                                                                                   
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2068, in _handle_dbapi_exception_noconnection                          
    util.raise_(                                                                                                                                       
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_                                                         
    raise exception                                                                                                                                    
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect                                            
    return fn()                                                                                                                                        
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 301, in connect                                                          
    return _ConnectionFairy._checkout(self)                                                                                                            
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 761, in _checkout                                                        
    fairy = _ConnectionRecord.checkout(pool)                                                                                                           
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 419, in checkout                                                         
    rec = pool._do_get()                                                                                                                               
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 145, in _do_get                                                          
    self._dec_overflow()                                                                                                                               
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__                                                   
    compat.raise_(                                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_                                                         
    raise exception                                                                                                                                    
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 142, in _do_get                                                          
    return self._create_connection()                                                                                                                   
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 247, in _create_connection                                               
    return _ConnectionRecord(self)                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 362, in __init__                                                         
    self.__connect()                                                                                                                                   
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect                                                        
    pool.logger.debug("Error on connect(): %s", e)                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__                                                   
    compat.raise_(                                                                                                                                     
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_                                                         
    raise exception                                                                                                                                    
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 599, in __connect                                                        
    connection = pool._invoke_creator(self)                                                                                                            
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect                                                      
    return dialect.connect(*cargs, **cparams)                                                                                                          
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect                                                     
    return self.dbapi.connect(*cargs, **cparams)                                                                                                       
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect                                                             
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)                                                                             
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "localhost" to address: Try again                           
                                                                                                                                                       
(Background on this error at: http://sqlalche.me/e/14/e3q8)                                                                                            
                                                                                                                                                       
Try rerunning the command with --debug to troubleshoot the error.

Desktop (please complete the following information):

  • OS: arch rolling
  • Browser (if applicable: N/A
  • Version of CLI: v1.100.1
  • Version of supabase-js (if applicable): N/A
  • Version of Node.js (if applicable): v20.5.1

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

Thanks for the discovery @kamilogorek !

I’ve also tried 127.0.0.1, my Mac IPv4 address and it’s DNS name :

root@docker-desktop:/# psql -h 192.168.1.89 -p 54328
psql: error: connection to server at "192.168.1.89", port 54328 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
root@docker-desktop:/# psql -h MBP-de-Arnaud-001 -p 54328
psql: error: connection to server at "MBP-de-Arnaud-001" (192.168.1.89), port 54328 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?

I found a workaround for my specific use case : my target db is in fact in a k8s cluster. I initially used kubectl proxy to establish a connection but I found another way to expose the db so it doesn’t use localhost anymore.