Skip to content
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

Non default database name causing errors for some sql commands #33

Open
em-c-rod opened this issue Feb 5, 2025 · 0 comments
Open

Non default database name causing errors for some sql commands #33

em-c-rod opened this issue Feb 5, 2025 · 0 comments

Comments

@em-c-rod
Copy link
Contributor

em-c-rod commented Feb 5, 2025

Entering the container for reference:
inspec shell -t docker://test_postgres --input-file ./crunchy-data-postgresql-stig-baseline/inputs_postgres14_example.yml

Error: Querying pgaudit.log_level without specifying the database name (this is what is written in the InSpec control V-233512):

sql.query('show pgaudit.log_level')

@desc="PostgreSQL query with error: show pgaudit.log_level",
@output="\npsql: error: connection to server at \"localhost\" (::1), port 5432 failed: FATAL:  database \"testuser\" does not exist\n">

Success: Querying pgaudit.log_level with specifying the database name:

sql.query('show pgaudit.log_level', [input('pg_db')])
@desc="PostgreSQL query: show pgaudit.log_level", @output="log"

cobbling some stuff together and using this bitnami image that has pgaudit installed by default:
docker run -it --name postgres-audit -e ALLOW_EMPTY_PASSWORD=yes -e POSTGRESQL_PGAUDIT_LOG=ALL docker.io/bitnami/postgresql:15
spins up postgres and enables pgaudit (see the line in the console saying 'pgaudit extension initialized')
$ docker exec -it postgres-audit psql -U postgres -A -t -c "SHOW pgaudit.log_level;"
log
runs that query of yours and returns a value without crashing like yours did
the line i'm running i actually pulled from this similar control from some vmware stig: https://www.stigviewer.com/stig/vmware_vsphere_8.0_vcenter_appliance_postgresql/2023-10-29/finding/V-259169 - the flags are basically just formatting things but you can look at it yourself if you want
the key thing to note i think is that i'm coming in as the postgres user and there is a postgres db created by default in the instance
$ docker exec -it postgres-audit psql -U postgres -A -t -c "\l"
postgres|postgres|UTF8|en_US.UTF-8|en_US.UTF-8||libc|
template0|postgres|UTF8|en_US.UTF-8|en_US.UTF-8||libc|=c/postgres
postgres=CTc/postgres
template1|postgres|UTF8|en_US.UTF-8|en_US.UTF-8||libc|=c/postgres
postgres=CTc/postgres
i think you are coming in as testuser and are not explicitly setting a database in which you are doing this. looking at the docs, i think if you don't explicitly provide a database name then it uses the username as the name of the database: see "Usage" section here near the end of that first paragraph: https://www.postgresql.org/docs/15/app-psql.html. and then as the error states, since there is no database containing the name testuser it blows up.
resolution would probably be to just provide the database name

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

No branches or pull requests

1 participant