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

psql attempts to connect to nonexistent database #46

Open
korydraughn opened this issue Dec 10, 2024 · 2 comments
Open

psql attempts to connect to nonexistent database #46

korydraughn opened this issue Dec 10, 2024 · 2 comments

Comments

@korydraughn
Copy link

korydraughn commented Dec 10, 2024

Bug

This was seen on the dev branch.

The following line assumes a database exists by the name of $IRODS_ICAT_DBUSER. If that database does not exist, an error will occur and the container will exit.

if [ "$( psql -h $IRODS_ICAT_DBSERVER -p $IRODS_ICAT_DBPORT -U $IRODS_ICAT_DBUSER \
-XtAc "SELECT 1 FROM pg_database WHERE datname='$IRODS_ICAT_DBNAME'" )" = '1' ]

The error output will look similar to the following:

catalog-1   | 2024-12-10 16:47:54.126 UTC [85] FATAL:  database "irods" does not exist               
provider-1  | psql: error: FATAL:  database "irods" does not exist                                   
provider-1  | Create iCAT database                                                                   
provider-1  | Password:                                                                              
provider-1  | createdb: error: database creation failed: ERROR:  permission denied to create database
catalog-1   | 2024-12-10 16:47:54.194 UTC [86] ERROR:  permission denied to create database          

To fix this, an existing database must be passed to psql. template1 seems like a decent option (see comments on this stackoverflow answer).

This situation can be reproduced by pointing the image to a new postgres database.

@mikkonie
Copy link
Contributor

Good catch. I've never ran into this myself, probably because we always initialize the postgres container with POSTGRES_DB in our Docker Compose network?

@korydraughn
Copy link
Author

I suspect your compose project is designed such that this isn't something anyone has to worry about. The good thing is that this is trivial to fix.

I think part of the reason for this happening is due to how I launched the postgres database. I used this:

# From docker-compose.yml

catalog:                                                                          
    build:                                                                        
        context: irods_catalog
    environment:
        - POSTGRES_PASSWORD=irods
    restart: always
# From irods_catalog/Dockerfile

FROM postgres:14                                                
                                                               
COPY init-user-db.sh /docker-entrypoint-initdb.d/init-user-db.sh
# From irods_catalog/init-user-db.sh

#!/bin/bash

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE DATABASE "ICAT";
    CREATE USER irods WITH PASSWORD 'irods';
    GRANT ALL PRIVILEGES ON DATABASE "ICAT" to irods;
EOSQL                                                                                   

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

2 participants