skip to Main Content

I’ve been trying to configure my m1 to work with an older ruby on rails api and I think in the process I’ve broken my ability to connect any of my python apis to their database images in docker running locally.

When I run:

psql -U dev -h localhost database

Instead of the lovely psql blinking cursor allowing me to run any sql statement I’d like I get this error message instaad:

psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  database "dev" does not exist

I’ve tried docker-compuse up and down and force recreating and brew uninstalling postgres and reinstalling postgres via brew. I’ve downloaded the postgres.app dmg and made sure to change it to a different port hoping that that would trigger the steps needed just for psycopg2 to connect to the docker image.

the docker-compose.yaml looks like this:

services:
  db:
    image: REDACTED
    container_name: db_name
    restart: always
    environment:
      POSTGRES_USER: dev
      POSTGRES_HOST_AUTH_METHOD: trust
    networks:
      default:
        aliases:
          - postgres
    ports:
    - 5432:5432

What am I missing and what can I blame ruby on rails for (which works by the way) 🤣

2

Answers


  1. Chosen as BEST ANSWER

    So the answer is pretty simple. What was happening was that I had a third instance of postgres running on my computer that I had not accounted for which was the brew version. Simply running brew services stop postgres and later brew uninstall postgres fixed all my problems with being able to have my ruby on rails api work which rely on "postgres native" on my mac (protip, I changed this one to use port 5431) and my python api work which use a containerized postgres on port 5432 without any headaches. During some intial confusion during my Ruby on Rails setup which required me getting Ruby 2.6.7 running on an m1 mac I must have installed postgres via brew in an attempt to get something like db:create to work.


  2. I think it’s just docker configuration you need to update
    First of all check your existing services in your local machine if the port is used by any other services (Mostly likely ylocal postgres server).
    next step is to change your yaml file as below

    services:
      db:
        image: REDACTED
        container_name: db_name
        restart: always
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_DB=test_db
        ports:
        - 5434:5432
    

    after that you can connect with following command in your cmd

    psql -U postgres -h localhost -p 5434
    

    assuming that you have separate yaml file for your python application
    if you merge your python code in same yaml file then your connection string will be your service name (db in your case) and port will be 5432

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search