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

chore: create appsmith schema for the pg branch with user and password #36540

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions deploy/docker/fs/opt/appsmith/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,51 @@ init_postgres() {
runEmbeddedPostgres=0
fi

# Create the appsmith schema
init_postgres() {
AnaghHegde marked this conversation as resolved.
Show resolved Hide resolved
echo "Initializing PostgreSQL..."

# Check if APPSMITH_DB_URL is a PostgreSQL URL
if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then
Copy link
Contributor

@abhvsn abhvsn Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now the entrupoint script which is responsible for creating an env file is starting with MongoDB url. How are we testing this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am testing this on the pg branch.

echo "APPSMITH_DB_URL is a valid PostgreSQL URL."

# Extract username, password, host, port, and database name from APPSMITH_DB_URL
DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p')
DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p')
DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p')
DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p')
DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p')

# Generate a random password if DB_USER or DB_PASSWORD are empty
if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then
DB_USER="postgres"
AnaghHegde marked this conversation as resolved.
Show resolved Hide resolved
DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password
echo "Generated random password for postgres user: $DB_PASSWORD"
fi

# Set defaults for host, port, and database name if not set
DB_HOST=${DB_HOST:-localhost}
DB_PORT=${DB_PORT:-5432}
DB_NAME=${DB_NAME:-appsmith}

echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER"

# Execute SQL to create the appsmith schema if it doesn't exist (single line)
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "CREATE SCHEMA IF NOT EXISTS appsmith;"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also create a separate user in case of embedded Postgres DB alongwith the password.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by separate user for embeded db? The user is hardcoded to appsmith when the username or password is empty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to create user statement, you can find more details https://www.postgresql.org/docs/8.0/sql-createuser.html


# Check if the schema creation was successful
if [ $? -eq 0 ]; then
echo "Schema 'appsmith' created or already exists."
else
echo "Failed to create schema 'appsmith'."
exit 1
fi

echo "PostgreSQL initialization completed."
else
echo "APPSMITH_DB_URL is either empty or not a PostgreSQL URL. Skipping PostgreSQL initialization."
fi
}
}

safe_init_postgres() {
Expand Down
Loading