繁体   English   中英

如何在Ubuntu中连接PostgreSQL

[英]How to connect PostgreSQL in Ubuntu

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

sudo apt-get install postgresql postgresql-contrib

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

database.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

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文档在这里 9.3 这里

我建议您遵循postgres教程

在本教程中,您应该创建一个用户和一个可选的数据库(如果没有,rails会为您创建数据库)。

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

验证您可以使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM