我试图连接一个ruby on rails项目有一个postgresql数据库,所以我可以部署在heroku当我完成。我试着自己挖掘答案,对postgresql和如何调试的知识非常有限,所以如果你对我能做些什么来解决这个问题有任何想法,请非常具体。
背景信息:我已经做了一个项目,没有问题,但这是一个新项目,我不得不卸载和重新安装postgresql,因为另一个错误,我现在有postgresql 12,我相信。
我刚开始一个新项目,当运行$rake db:create
时,我得到以下错误:
Database 'gachamons_backend_development' already exists
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Couldn't create 'gachamons_backend_test' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/rails:5:in `<top (required)>'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:10:in `require'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:10:in `block in <top (required)>'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:7:in `tap'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:7:in `<top (required)>'
Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/rails:5:in `<top (required)>'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:10:in `require'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:10:in `block in <top (required)>'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:7:in `tap'
/mnt/c/users/adam/desktop/flatiron/gachamons-backend/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
当我尝试使用$service postgresql status
查看服务器是否关闭时:
12/main (port 5432): down
$psql
:
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
我从另一个帖子看到运行$dpkg -l | grep postgres
产生这个输出,我不明白如何调试:
ii postgresql 12+214 all object-relational SQL database (supported version)
ii postgresql-12 12.2-4 amd64 object-relational SQL database, version 12 server
ii postgresql-client-12 12.2-4 amd64 front-end programs for PostgreSQL 12
ii postgresql-client-common 214 all manager for multiple PostgreSQL client versions
ii postgresql-common 214 all PostgreSQL database-cluster manager
ii postgresql-contrib 12+214 all additional facilities for PostgreSQL (supported version)
然后我运行$pg_lsclusters
:
12 main 5432 down postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
最后$sudo service postgresql restart
* Restarting PostgreSQL 12 database server * Error: /usr/lib/postgresql/12/bin/pg_ctl /usr/lib/postgresql/12/bin/pg_ctl start -D /var/lib/postgresql/12/main -l /var/log/postgresql/postgresql-12-main.log -s -o -c config_file="/etc/postgresql/12/main/postgresql.conf" exited with status 1:
2021-08-24 15:48:39.458 EDT [4148] LOG: starting PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
2021-08-24 15:48:39.460 EDT [4148] LOG: could not bind IPv4 address "127.0.0.1": Permission denied
2021-08-24 15:48:39.460 EDT [4148] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2021-08-24 15:48:39.460 EDT [4148] WARNING: could not create listen socket for "localhost"
2021-08-24 15:48:39.460 EDT [4148] FATAL: could not create any TCP/IP sockets
2021-08-24 15:48:39.461 EDT [4148] LOG: database system is shut down
pg_ctl: could not start server
Examine the log output.
编辑以包含数据库。yml:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: gachamons_backend_development
host: localhost
port: 5432
user: postgres
password: postgres
test:
<<: *default
database: gachamons_backend_test
port: 5432
user: postgres
password: postgres
production:
<<: *default
url: <%= ENV['MY_APP_DATABASE_URL'] %>
这些都是我试图调试的东西,但我不太熟悉,无法理解输出的含义。任何详细的解释,以便我下次能理解,我将不胜感激,让我从a到b。非常感谢。
从上面的输出可以看出,postgresql数据库已经关闭。在postgresql配置文件(/etc/postgresql/12/main/postgresql.conf)中输入:listen_addresses = ' * '
重新启动postgresql。
user @engineersmnky指出了在数据库中的测试环境。Yml没有主机,在尝试创建数据库时产生多个错误。