0

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
1
  • Install pgadmin Commented Sep 9, 2015 at 9:46

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

I tried sudo su – postgres and pressed enter it gave me the result No password entry for '-'.
Yes please change sign to -. It was -. Try copy and paste this. sudo su - postgres .
Thank you but when i typed next command createuser it gave me this errorcreation of new role failed : Error : role 'postgres' already exist.
Dont try to create a new user with name postgres it already exist. Try give other name to new user. When Enter name of role to add: prompt try give name other than postgres
@ Dipak : After running createuser command it is throwing the avobe error.Ok if it has already username then how can i create another new user.I need the command.
|
0

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.

6 Comments

@ Fabio :I did as per you but when tried to run command rake db:create,It is giving this error.Is the server running on the host 10.25.25.100 and accepting the TCP/IP connection on port 5432.Where my server is running on the same ip address.I am updating my database.yml file above.
I can not open this pg_hba.conf in ssh.Please can you provide me the command to open this.?
you might have to use vim ( or other linux text editors) from your ssh session. if you are a windows user, you could try using WinSCP, so you have a GUI to deal with and can manipulate the file in your local box.
my postgresql version is 9.3.When i got into this /etc/postgresql/9.3/main/ and search that file pg_hba.conf it was not present.
what files do you have in this folder?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.