Ruby on Rails: How to Edit Database.Yml for Postgresql

Ruby on Rails: How can I edit database.yml for postgresql?

Simply:

development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
host: localhost

Source: Configuring Rails Applications

changing database.yml for postgresql

The following should work (assuming the user & password are correct in postgres)

default: &default
adapter: postgresql
user: myrubyblog
password:
pool: 5
timeout: 5000

development:
<<: *default
database: myrubyblog

test:
<<: *default
database: myrubyblog

See What do the &,<<, * mean in this database.yml file? for how &default is re-used for each of the database definitions.

You don't need to define a production env in your database.yml; heroku will create its own version of database.yml for use in production.

See http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database for more details about the configuration in database.yml

Running rails new myblogapp --database=postgresql will create a new myblogapp application with the appropriate content in database.yml customized for using with postgres.

Changing sqlite to PostgreSQL in ruby on rails

Make sure you have installed PostgreSQL in your machine and add a helpful tool called pgAdmin, then update the database.yml file like below

default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: xxxxx #<-- which you have defiend while installing postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
<<: *default
database: project_development

test:
<<: *default
database: project_test

production:
<<: *default
database: project_production
username: username
password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>

and gem pg use in your Gemfile like gem 'pg', '~> 0.20.0' with this version then run bundle install

rake db:create
rake db:migrate

for Heroku

heroku run rake db:migrate

and you can use rails command instead of rake if your rails version is rails > 5.0.0

How can I change from SQLite database to PostgreSQL database in a working Rails project for localhost?

You need to install postgresql package into your system you can refer to this link for instruction regarding installing postgres all you need to do is execute the following commands

sudo apt-get install postgresql postgresql-contrib

this installs postgres into your system

then you need to create a user with the details that you have mentioned in the database.yml file of your rails application

You can use pgadmin for db browser you can refer to this question for other tools

to install pgadmin3 run the following command

 sudo apt-get install pgadmin3

then configure it with the details like host port etc that you are using(refer to database.yml)

Adding Postgresql to an existing Rails project

Rename "Videos" table to "videos".

In PostgreSQL unquoted names are case-insensitive. Thus SELECT * FROM videos and SELECT * FROM ViDeOs are equivalent.

However, quoted names are case-sensitive. SELECT * FROM "videos" is not equivalent to SELECT * FROM "Videos".

More details here.

How to setup database.yml to connect to Postgres Docker container?

You could set a correct environment variable first, and access it from your database.yml:

host: <%= ENV['POSTGRES_IP'] %>

With a bashrc like (using bash substring removal):

export DOCKER_HOST=$(docker-machine env default)
export POSTGRES_IP=${DOCKER_HOST#tcp://}

How does Rails PostgreSQL automatically creates user access in tutorials and in Heroku even though not explicitly defined in database.yml

There are two main ways to access a local postgresql database. One is using a specific user/password combination in the database.yml file. The other is to set up local postgres to trust your user identity.

For the latter case you need to edit the pg_hba.conf file to have either a trust line (allow anyone to connect, just pass the username) or a peer or ident (depending on the connection - socket or tcp - allows only a postgres role that has the same name as the connecting user, in your case probably webmaster)

For example:

# Allow any user on the local system to connect to any database with
# any database user name using Unix-domain sockets (the default for local
# connections).
#
# TYPE DATABASE USER ADDRESS METHOD
local all all trust

# The same using local loopback TCP/IP connections.
#
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust

You can find more examples here:
https://www.postgresql.org/docs/devel/auth-pg-hba-conf.html

WARNING do not use this config on a production server!



Related Topics



Leave a reply



Submit