简体   繁体   中英

How to connect PostgreSQL in Ubuntu

I installed PostgreSQL database on my Ubuntu server by the help of the below command

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.

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.

For 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 :

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.

For 9.4 document here . 9.3 here

I would advise you to follow a postgres tutorial .

In this tutorial you should create a user and optionally a database (rails will create the db for your if you don't).

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

Verify that you can connect to your database with pqsl (it is part of the tutorial). after that, you can configure your rails app as follows:

file /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

Update:

Based on your comment, I would also advise you to:

In postgres file postgresql.config , adjust the listening configuration to:

# - Connection Settings -

listen_addresses = 'localhost,10.25.25.100'

In postgres file pg_hba.conf , add the proper network config for your rails app. It is probably:

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

After that you have to restart your postgres server.

And finally you can try running rake db:create again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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