【问题标题】:How to connect PostgreSQL in Ubuntu如何在 Ubuntu 中连接 PostgreSQL
【发布时间】:2015-09-10 13:37:47
【问题描述】:

我通过以下命令在我的 Ubuntu 服务器上安装了 PostgreSQL 数据库

sudo apt-get install postgresql postgresql-contrib

但是在这里我不知道如何连接这个 PostgreSQL 数据库以及如何设置它的用户名、密码、主机和端口。因为我将在我的 Rails 项目中使用这个数据库,所以这些东西是必需的。请帮助我设置这个数据库。

数据库.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  database: 100salons
  username: sallon
  password: 12345
  host: 10.25.25.100
  port: 5432
  pool: 5

development:
  <<: *default
  database: 100salons_dev
  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: 100salons

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: 100salons_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: 100salons_prod

【问题讨论】:

标签: ruby-on-rails postgresql ubuntu


【解决方案1】:

Postgresql 有不同的命令来创建不同版本的密码和用户名。

对于 9.3:

对于创建用户类型命令:

sudo su – postgres ## This will take you to postgres user

createuser  ## To create new user

上面的命令提示符:

Enter name of role to add: newuser
Shall the new role be a superuser? (y/n) y

为新创建的用户类型创建密码:

createuser --pwprompt

对于 9.4:

输入简单的命令:

createuser -P -s -e joe ## joe is username here

这将输出如下:

 Enter password for new role: xyzzy
 Enter it again: xyzzy
 CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

你已经完成了。

对于 9.4 文档here。 9.3here

【讨论】:

  • 我尝试了sudo su – postgres 并按下回车键给了我结果No password entry for '-'
  • 是的,请将符号更改为 -。它是 -。尝试复制并粘贴此内容。 sudo su - postgres .
  • 谢谢,但是当我输入下一个命令createuser时,它给了我这个错误creation of new role failed : Error : role 'postgres' already exist
  • 不要尝试创建一个名为 postgres 的新用户,它已经存在。尝试为新用户提供其他名称。当Enter name of role to add: 提示时尝试使用 postgres 以外的名称
  • @ Dipak :运行createuser 命令后,它会抛出avobe 错误。好的,如果它已经有用户名,那么我该如何创建另一个新用户。我需要该命令。
【解决方案2】:

我建议您关注postgres tutorial

在本教程中,您应该创建一个用户和一个可选的数据库(如果您不这样做,rails 将为您创建数据库)。

端口和用户权限的postgres配置需要在文件夹/etc/postgresql/9.3/main中的文件postgresql.configpg_hba.conf中完成

验证您是否可以使用pqsl 连接到您的数据库(它是教程的一部分)。 之后,您可以按如下方式配置您的 Rails 应用程序:

文件/your-app-path/config/database.yml:

default: &default
  adapter: postgresql
  encoding: utf8
  host: <name/ipAddress of db host or "localhost">
  port: 5432
  username: <your postgres user>
  password: <your postgres user password>

development:
  <<: *default
  database: railsApp_dev

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: railsApp_test

production:
  <<: *default
  database: railsApp_prod

这应该让你开始。

如果您需要更多帮助,请告诉我们

更新:

根据您的评论,我还建议您:

在postgres文件postgresql.config中,调整监听配置为:

# - Connection Settings -

listen_addresses = 'localhost,10.25.25.100'

在 postgres 文件 pg_hba.conf 中,为您的 rails 应用添加正确的网络配置。大概是:

# IPv4 local connections:
host    all             all             10.25.25.100/32            md5

之后,您必须重新启动您的 postgres 服务器。

最后你可以尝试再次运行rake db:create

【讨论】:

  • @Fabio:我按照你的要求做了,但是当尝试运行命令 rake db:create 时,它​​给出了这个错误。Is the server running on the host 10.25.25.100 and accepting the TCP/IP connection on port 5432。我的服务器在同一个 IP 地址上运行。我在更新我上面的 database.yml 文件。
  • 我无法在 ssh 中打开这个pg_hba.conf。请你提供打开这个的命令吗?
  • 您可能必须在 ssh 会话中使用 vim(或其他 linux 文本编辑器)。如果您是 windows 用户,您可以尝试使用 WinSCP,这样您就有了一个 GUI 来处理并可以在本地框中操作文件。
  • 我的 postgresql 版本是 9.3。当我进入这个 /etc/postgresql/9.3/main/ 并搜索那个文件 pg_hba.conf 它不存在。
  • 这个文件夹里有什么文件?
猜你喜欢
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 2020-11-23
  • 2012-01-26
  • 1970-01-01
相关资源
最近更新 更多