title: How do I access postgresql within Docker with sqlalchemy?
body: <p>I'm trying to interact with a dockerized PostgreSQL server using SQLAlchemy. Something like:</p>
  
  <pre><code>engine = create_engine('postgresql://user:user_password@localhost:5432/database')
  df.to_sql('table', engine)
  </code></pre>
  
  <p>Which gives me this error:</p>
  
  <blockquote>
    <p>OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?</p>
  </blockquote>
  
  <p>Which suggests the Docker postgresql (which is running) isn't available at that port. I've tried adding <code>-p 5432:5432</code> to my <code>docker-compose exec</code> without success. Any tips? </p>