[英]How to connect PostgreSQL in Ubuntu
I installed PostgreSQL database on my Ubuntu server by the help of the below command 我通过以下命令在Ubuntu服务器上安装了PostgreSQL数据库
sudo apt-get install postgresql postgresql-contrib
But here i can not know how to connect with this PostgreSQL database and how to setup its username,password,host and port.As i am going to use this database with my Rails project these things are required.Please help me to setup this database. 但是在这里我不知道如何连接这个PostgreSQL数据库以及如何设置它的用户名,密码,主机和端口。由于我要在我的Rails项目中使用这个数据库,所以需要这些东西。请帮助我设置这个数据库。
database.yml:
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 has different command to create password and username in different version. PostgreSQL有不同的命令来创建不同版本的密码和用户名。
For 9.3 : 对于9.3:
For create a user type commands: 要创建用户,请键入命令:
sudo su – postgres ## This will take you to postgres user
createuser ## To create new user
Above command prompt like : 上面的命令提示符如下:
Enter name of role to add: newuser
Shall the new role be a superuser? (y/n) y
To create password for newly created user type: 要为新创建的用户类型创建密码:
createuser --pwprompt
For 9.4 : 对于9.4:
Type simple command : 输入简单命令:
createuser -P -s -e joe ## joe is username here
This will output like : 这将输出如下:
Enter password for new role: xyzzy
Enter it again: xyzzy
CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
You are done. 大功告成
I would advise you to follow a postgres tutorial . 我建议您遵循postgres教程 。
In this tutorial you should create a user and optionally a database (rails will create the db for your if you don't). 在本教程中,您应该创建一个用户和一个可选的数据库(如果没有,rails会为您创建数据库)。
postgres configuration for port and user permissions need to be done in the files postgresql.config
and pg_hba.conf
from folder /etc/postgresql/9.3/main
需要在
/etc/postgresql/9.3/main
文件夹中的文件postgresql.config
和pg_hba.conf
完成端口和用户权限的postgres配置。
Verify that you can connect to your database with pqsl
(it is part of the tutorial). 验证您可以使用
pqsl
连接到数据库(它是本教程的一部分)。 after that, you can configure your rails app as follows: 之后,您可以按以下方式配置Rails应用程序:
file /your-app-path/config/database.yml
: 文件
/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
This should get you started. 这应该使您入门。
Let us know if you need more help 让我们知道您是否需要更多帮助
Based on your comment, I would also advise you to: 根据您的评论,我还建议您:
In postgres file postgresql.config
, adjust the listening configuration to: 在postgres文件
postgresql.config
,将侦听配置调整为:
# - Connection Settings -
listen_addresses = 'localhost,10.25.25.100'
In postgres file pg_hba.conf
, add the proper network config for your rails app. 在postgres文件
pg_hba.conf
,为您的rails应用添加正确的网络配置。 It is probably: 可能是:
# IPv4 local connections:
host all all 10.25.25.100/32 md5
After that you have to restart your postgres server. 之后,您必须重新启动postgres服务器。
And finally you can try running rake db:create
again. 最后,您可以尝试再次运行
rake db:create
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.