Github Actions Error Postgres User for rspec testing - postgresql

So I am trying to run rspec tests for our Ruby applications and we are using Github actions
- name: Create additional user postgres w password postgres & restart service
run: |
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
sudo systemctl restart postgresql.service
- name: Run Rspec
env:
PGPORT: 5433
PGUSER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_PORT: 5433
run: BUNDLE_GITHUB__COM=${{ secrets.BUNDLE_GITHUB__COM }} rspec
I have this running, it gets all the way down to running rspec then I received this error
An error occurred while loading ./spec/auth/auth_strategies/base_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::ConnectionNotEstablished:
connection to server at "localhost" (::1), port 5433 failed: FATAL: password authentication failed for user "postgres"
Any thoughts?

Related

Cannot psql into docker postgres image port forwarded locally

I'm continuously hitting an error when trying to psql into a docker composed postgres image that has its ports forwarded. (this issue seems to persist also when attempting to access the DB programatically via node application).
Running docker-compose up -d on the following docker compose file:
services:
postgres:
container_name: cnc-matches
image: postgres:12.1-alpine
ports:
- '5432:5432'
environment:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: pass
POSTGRES_DB: cnc-matches
When running psql to attempt to access it I hit the following error continuously:
C:\Users\danie\Desktop\dev\cnc-db\db-setup>psql -h "localhost" -p "5432" -U dbuser
Password for user dbuser: pass
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "dbuser"
When running docker exec I'm able to access the table and info fine:
C:\Users\danie\Desktop\dev\cnc-db\db-setup>docker exec -it cnc-matches psql -U dbuser cnc-matches
psql (12.1)
Type "help" for help.
cnc-matches=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dbuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
I've tried creating a new user as well as altering the dbuser profiles passwords in here with ALTER PASSWORD dbuser WITH PASSWORD 'pass' and I still cannot access the db with default psql command locally.
cnc-matches=# CREATE USER tester WITH PASSWORD 'tester';
CREATE ROLE
cnc-matches=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dbuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
tester | | {}
C:\Users\danie\Desktop\dev\cnc-db\db-setup>psql -h "localhost" -p "5432" -U tester
Password for user tester: tester
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "tester"
Not sure what it is I'm misisng here, if relevant running via windows 11 cmd.
Any help/suggestions appreciated.
Looks like you also need to include the database name when connecting to the database running in the Docker container, ex
psql -h "0.0.0.0" -p "5432" -U dbuser cnc-matches
If the container was successfully started, try running:
psql -U dbuser -d cnc-matches -h 0.0.0.0 --port 5432
For the port use whatever you mapped yours in the container to; in your case 5432.
You must specify all that explicitly or else you will get an error that the database cannot accept TCP/IP connections.
First time I am posting, sorry if there is wrong formatting.
I updated your docker-compose file with a network bridge as still isolated from the network with the host machine.
Steps:
"db-net" can be any name you want but both must coincide with your docker compose file
Create a docker network with: docker network create db-net
Run: docker-compose up -d
version: "3.9"
services:
postgresCNC:
container_name: cnc-matches
image: postgres:15.0-alpine
ports:
- '5432:5432'
environment:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: pass
POSTGRES_DB: cnc-matches
networks:
db-net:
networks:
db-net:
driver: bridge
Network bridge by default uses the hardware network device to communicate between containers and host machine as with containers with others containers.
More info here:
https://docs.docker.com/network/bridge/

postgres docker : role "root" does not exist

I am running docker-compose in Github-Action. docker-compose.yml has following service definition for postgres
postgres:
container_name: postgres
image: postgres:12
restart: always
volumes:
- ./test/data/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
POSTGRES_DB: "pgdb"
POSTGRES_USER: "pguser"
POSTGRES_PASSWORD: "fr2Yitl4BgX"
ports:
- "${POSTGRES_PORT:-5432}:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
networks:
- "local-api"
But when container start on serf-hosted Github-Action runner, I see following
postgres | 2021-12-02 19:48:33.537 UTC [414] FATAL: role "root" does not exist
postgres | 2021-12-02 19:48:43.984 UTC [424] FATAL: role "root" does not exist
postgres | 2021-12-02 19:48:54.265 UTC [433] FATAL: role "root" does not exist
postgres | 2021-12-02 19:49:04.410 UTC [443] FATAL: role "root" does not exist
What is missing here ?
should set POSTGRES_DB and POSTGRES_USER in healthcheck
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
I'm getting a similar error when trying to run a pg_restore on the postgresql docker container, I had set up postgresql on docker-compose and was trying to restore a db (from my prev postgresql) to it. Just had the initial user as "postgres". Some errors I'd get:
pg_restore: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "root" does not exist
createuser: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
On research I found my pg_restore command needed to specify the User for it to work (tried specifying role, that failed)
Here's the working pg_restore command. Perhaps you can do similarly and find a way to specify user in yours?
#on docker container of psql, command that failed
pg_restore --verbose --clean --no-acl --no-owner -h localhost --role=postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup
#command that worked
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup
# lastly (for those doing a pg_restore), from psql commandline, if the restore seems to have worked but don't see data, you may be on the wrong db
\c
\c theDbToSwitchTo

psql: error: could not connect to server: FATAL: password authentication failed for user while connecting to dockerized postgres image

I tried to access my Postgres DB that running in docker, here is my docker-compose.yml:
Version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=fdc_dashboard
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=saya1234
It successfully run using:
docker-compose up db
but when I tried to connect to it
psql -h localhost -p 5432 -d fdc_dashboard -U admin --password
It raised:
psql: error: could not connect to server: FATAL: password authentication failed for user "admin"
Is there something that I missed?

psql: error: could not connect to server: FATAL: role "postgres" does not exist

I have "dockerized" a Django/PostgreSQL app and try to connect to my database
I have 2 containers: web et db
It works but I can't connect to my postgresql database
I used to ran docker exec -it coverage_africa_db_1 psql -U postgres but I got an error
psql: error: could not connect to server: FATAL: role "postgres" does not exist
I try to 'jump' into my container by running the command docker exec -it aab213f730cd bash and try to connect using psql command...
psql -d db_dev
psql: error: could not connect to server: FATAL: role "root" does not exist
or
psql -U postgres
error: could not connect to server: FATAL: role "postgres" does not exist
in fact, none of psql options works...
.env.dev
SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
restart: always
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
restart: always
volumes:
- postgres_data:/var/lib/postgres/data/
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
volumes:
postgres_data:
With postgres container, this:
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
defines how the database is initialized. If you didn't change it, you should be able to connect as user 'user' with password 'user'.
If you did change it, then the actual values are those which were present at the first launch. After first launch those credentials are written into the database, which data is on postgres_data volume. If you want to delete the data and reinitialize database with new credentials, use docker-compose down -v.

How to connect to postgres created with docker-compose from outside host

I think this problem should be very easy to solve, but I am pulling my hair out. I have the following docker-compose:
version: "3"
services:
pg-db:
image: postgres:11
environment:
POSTGRES_PASSWORD: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
ports:
- 5432:5432
#network_mode: host
Then I run docker-compose up and it starts the container
pg-db_1_78e7ec4a95e6 | 2020-02-21 13:53:53.928 UTC [1] LOG: database system is ready to accept connections
I can connect to it with docker exec
docker exec -it docker_pg-db-compose_1_78e7ec4a95e6 psql -h pg-db -U postgres postgres
But I can't manage to connect to it by 'naked' psql:
psql postgresql://postgres:postgres#localhost:5432/postgres
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
psql postgresql://postgres:postgres#pg-dg:5432/postgres
psql: could not translate host name "pg-db" to address: Name or service not known
I've tried with network_mode but it doesn't help.
After starting docker-compose, you need to get the docker container ip by doing this:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Then you can do postgresql://postgres:postgres#<container idp>:5432/postgres you should be able to connect to it
I'm using a modified version of the docker-compose file provided in the Postgres DockerHub page.
version: '3.1'
services:
db:
image: postgres:11
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres
When I run docker-compose up I get a very similar "ready" message:
db_1 | 2020-02-21 14:34:17.826 UTC [1] LOG: database system is ready to accept connections
At this point, I can connect to Postgres using psql.
$ psql -h localhost -U postgres
Password for user postgres:
psql (12.2, server 11.7 (Debian 11.7-1.pgdg90+1))
Type "help" for help.
postgres=#
I'm also able to connect using the connection string that you provided, and postico:
The differences are subtle, I'm not sure if the restart policy makes any difference here, but give it a try.