Skip to content

./run psql: connect to specified database #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kennyhei
Copy link

@kennyhei kennyhei commented Jun 11, 2025

./run psql command now connects to database that has been set in .env. By default the postgres image uses POSTGRES_USER as db name if POSTGRES_DB is not specified and likewise psql -U [user_name] connects to database [user_name] if database has not been specified with -d flag.

This fixes the issue where username and database name differ.

Note that if POSTGRES_DB is commented out / does not exist in .env, then the script works the old way (psql -U [user_name]).

@nickjj
Copy link
Owner

nickjj commented Jun 11, 2025

Hi,

Note that if POSTGRES_DB is commented out / does not exist in .env, then the script works the old way (psql -U [user_name])

I didn't test this locally but if -d "${POSTGRES_DB}" evaluates where POSTGRES_DB is undefined this will produce -d with no value right? What happens in the psql case when -d is passed no parameter, or does the Postgres image automatically define POSTGRES_DB with the user internally so there's always a value?

@kennyhei
Copy link
Author

kennyhei commented Jun 12, 2025

Yes, it produces -d with no value. Weird, it works and then just uses the same database name as what's POSTGRES_USER but if I directly try to input the evaluated command to terminal:

docker compose exec postgres psql -d -U postgres

It results in

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "-U" does not exist

I can modify the script to specify -d only if POSTGRES_DB has value

@kennyhei
Copy link
Author

I updated the script to include -d flag only if POSTGRES_DB is set

@nickjj
Copy link
Owner

nickjj commented Jun 12, 2025

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "-U" does not exist

That is what I thought would happen since it tries to pass -U in as an arg to -d. You can run docker compose exec postgres env to see what's available. The Postgres image does not always populate POSTGRES_DB.

@nickjj
Copy link
Owner

nickjj commented Jun 12, 2025

I'd have to also test this but I think Docker Compose now supports the same type of nested variable interpolation with a default so we can also do the same in the compose.yaml file so the POSTGRES_DB field doesn't need to be commented out by default.

Do you want to test that?

@kennyhei
Copy link
Author

I'd have to also test this but I think Docker Compose now supports the same type of nested variable interpolation with a default so we can also do the same in the compose.yaml file so the POSTGRES_DB field doesn't need to be commented out by default.

Do you want to test that?

Did the change and seems to work based on testing 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants