You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Termux中为Rails应用启用PostgreSQL及连接问题排查

解决Termux中Rails执行rails db:create的PostgreSQL连接错误

1. 确认PostgreSQL服务状态

Termux中PostgreSQL默认不会自动启动,按以下步骤处理:

  • 首次使用需先初始化数据库:
    initdb $PREFIX/var/lib/postgresql
    
  • 启动PostgreSQL服务:
    pg_ctl start -D $PREFIX/var/lib/postgresql
    

2. 修正Rails数据库配置

打开项目根目录下的config/database.yml,确保PostgreSQL的socket路径与Termux实际路径匹配:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  socket: /data/data/com.termux/files/usr/tmp/.s.PGSQL.5432

3. 可选:设置PostgreSQL开机自启

避免每次打开Termux都手动启动服务,可将启动命令加入bash配置:

echo "pg_ctl start -D $PREFIX/var/lib/postgresql" >> ~/.bashrc

内容的提问来源于stack exchange,提问作者Yeison Valencia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:05:52