Skip to content

Commit a1d67b6

Browse files
committed
fix: Don't explicitly use a socket for Postgres
Previously, the default configuration for flox/postgres uses both a socket and a TCP connection. This was both redundant and caused Postgres not to start in several scenarios -- primarily because the postgres socket file path was deep in the .flox structure, which already might be deep in the filesystem. The maximum lenght for a file socket path on MacOS is 103 characters. If it's longer than that, PG will not start. This change simply removes the call to `-c socket` because it's redundant with the way Postgres is already configured via this env. Additionally, the presence of PGHOST cause the primary go Postgres library to panic as it tries to parse that even when the presence of TCP connection information is available. I actually wonder if PGHOST should be renamed to PGRUN or something else, as it's only used to locate the `/run` directory.
1 parent acb2486 commit a1d67b6

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

postgres/.flox/env.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
{
2-
"name": "postgres",
3-
"version": 1
4-
}
1+
{"owner":"stahnma","name":"postgres","floxhub_url":"https://hub.flox.dev/","version":1}

postgres/.flox/env.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rev": "9828552766d58320783433f6279433a634e8ed3c",
3+
"local_rev": "34a6951f28378f39cb1f2456157e9e1144e72278",
4+
"version": 1
5+
}

postgres/.flox/env/manifest.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"PGUSER": "pguser"
1919
},
2020
"hook": {
21-
"on-activate": "\nexport PGDIR=\"$FLOX_ENV_CACHE/postgres\"\nexport PGDATA=$PGDIR/data\nexport PGHOST=$PGDIR/run\nexport PGCONFIGFILE=\"$PGDIR/postgresql.conf\"\nexport LOG_PATH=$PGHOST/LOG\nexport SESSION_SECRET=\"$USER-session-secret\"\nexport DATABASE_URL=\"postgresql:///$PGDATABASE?host=$PGHOST&port=$PGPORT\"\n\nif [[ ! -d \"$PGHOST\" ]]; then\n mkdir -p \"$PGHOST\"\nfi\n\nif [[ ! -d \"$PGDATA\" ]]; then\n mkdir -p \"$PGDATA\"\n pg_initdb() {\n initdb \"$PGDATA\" \\\n --locale=C \\\n --encoding=UTF8 \\\n -A md5 \\\n --auth=trust \\\n --username $PGUSER \\\n --pwfile=<(echo $PGPASS)\n }\n export -f pg_initdb # This is needed for gum to be able to call function\n if [[ \"$FLOX_ENVS_TESTING\" == \"1\" ]]; then\n pg_initdb\n else\n gum spin --spinner dot --title \"Running initdb in $PGDATA\" -- bash -c pg_initdb\n fi\n echo \"✅ Initialize PostgreSQL ($PGDATA)\"\n\nfi\n\n\n#\nif [[ ! -f \"$PGCONFIGFILE\" ]]; then\n tee -a $PGCONFIGFILE > /dev/null << EOF\nlisten_addresses = '$PGHOSTADDR';\nport = '$PGPORT';\nunix_socket_directories = '$PGHOST';\nunix_socket_permissions = '0700';\nEOF\n echo \"✅ Configure PostgreSQL ($PGCONFIGFILE)\"\nfi\n\npg_ctl -D \"$PGDATA\" -w start -o \"-c unix_socket_directories=$PGHOST -c listen_addresses=$PGHOSTADDR -p $PGPORT\" > /dev/null\nif psql -lqt | cut -d \\| -f 1 | grep -qw $PGDATABASE; then\n echo \"✅ Database '$PGDATABASE' already exists\"\nelse\n createdb\n echo \"✅ Database '$PGDATABASE' created\"\nfi\npg_ctl -D \"$PGDATA\" -m fast -w stop > /dev/null\n\ngum style --border double --margin \"1 2\" --padding \"1 4\" \\\n 'Start PostgreSQL in the background:' \\\n ' 👉 flox services start' \\\n ' 👉 flox activate --start-services' '' \\\n 'Try to connect to PostgreSQL:' \\\n ' 👉 psql' '' \\\n 'Connection information:' \\\n \" listen_addresses=$PGHOSTADDR\" \\\n \" port=$PGPORT\"\n\n"
21+
"on-activate": "\nexport PGDIR=\"$FLOX_ENV_CACHE/postgres\"\nexport PGDATA=$PGDIR/data\nexport PGHOST=$PGDIR/run\nexport PGCONFIGFILE=\"$PGDIR/postgresql.conf\"\nexport LOG_PATH=$PGHOST/LOG\nexport SESSION_SECRET=\"$USER-session-secret\"\nexport DATABASE_URL=\"postgresql:///$PGDATABASE?host=$PGHOST&port=$PGPORT\"\n\nif [[ ! -d \"$PGHOST\" ]]; then\n mkdir -p \"$PGHOST\"\nfi\n\nif [[ ! -d \"$PGDATA\" ]]; then\n mkdir -p \"$PGDATA\"\n pg_initdb() {\n initdb \"$PGDATA\" \\\n --locale=C \\\n --encoding=UTF8 \\\n -A md5 \\\n --auth=trust \\\n --username $PGUSER \\\n --pwfile=<(echo $PGPASS)\n }\n export -f pg_initdb # This is needed for gum to be able to call function\n if [[ \"$FLOX_ENVS_TESTING\" == \"1\" ]]; then\n pg_initdb\n else\n gum spin --spinner dot --title \"Running initdb in $PGDATA\" -- bash -c pg_initdb\n fi\n echo \"✅ Initialize PostgreSQL ($PGDATA)\"\n\nfi\n\n\n#\nif [[ ! -f \"$PGCONFIGFILE\" ]]; then\n tee -a $PGCONFIGFILE > /dev/null << EOF\nlisten_addresses = '$PGHOSTADDR';\nport = '$PGPORT';\nunix_socket_directories = '$PGHOST';\nunix_socket_permissions = '0700';\nEOF\n echo \"✅ Configure PostgreSQL ($PGCONFIGFILE)\"\nfi\n\npg_ctl -D \"$PGDATA\" -w start -o \"-c listen_addresses=$PGHOSTADDR -p $PGPORT\" > /dev/null\nif psql -lqt | cut -d \\| -f 1 | grep -qw $PGDATABASE; then\n echo \"✅ Database '$PGDATABASE' already exists\"\nelse\n createdb\n echo \"✅ Database '$PGDATABASE' created\"\nfi\npg_ctl -D \"$PGDATA\" -m fast -w stop > /dev/null\n\ngum style --border double --margin \"1 2\" --padding \"1 4\" \\\n 'Start PostgreSQL in the background:' \\\n ' 👉 flox services start' \\\n ' 👉 flox activate --start-services' '' \\\n 'Try to connect to PostgreSQL:' \\\n ' 👉 psql' '' \\\n 'Connection information:' \\\n \" listen_addresses=$PGHOSTADDR\" \\\n \" port=$PGPORT\"\n\n"
2222
},
2323
"options": {
2424
"systems": [
@@ -30,7 +30,7 @@
3030
},
3131
"services": {
3232
"postgres": {
33-
"command": "postgres -D $PGDATA -c unix_socket_directories=$PGHOST -c listen_addresses=$PGHOSTADDR -p $PGPORT"
33+
"command": "postgres -D $PGDATA -c listen_addresses=$PGHOSTADDR -p $PGPORT"
3434
}
3535
}
3636
},

postgres/.flox/env/manifest.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ EOF
7070
echo "✅ Configure PostgreSQL ($PGCONFIGFILE)"
7171
fi
7272
73-
pg_ctl -D "$PGDATA" -w start -o "-c unix_socket_directories=$PGHOST -c listen_addresses=$PGHOSTADDR -p $PGPORT" > /dev/null
73+
pg_ctl -D "$PGDATA" -w start -o "-c listen_addresses=$PGHOSTADDR -p $PGPORT" > /dev/null
7474
if psql -lqt | cut -d \| -f 1 | grep -qw $PGDATABASE; then
7575
echo "✅ Database '$PGDATABASE' already exists"
7676
else
@@ -92,7 +92,7 @@ gum style --border double --margin "1 2" --padding "1 4" \
9292
'''
9393

9494
[services]
95-
postgres.command = "postgres -D $PGDATA -c unix_socket_directories=$PGHOST -c listen_addresses=$PGHOSTADDR -p $PGPORT"
95+
postgres.command = "postgres -D $PGDATA -c listen_addresses=$PGHOSTADDR -p $PGPORT"
9696

9797

9898
[options]

0 commit comments

Comments
 (0)