Setting up Rails project on localhost, postgresql issue - ruby-on-rails

Just trying to set up rails app on ubuntu to do some low level dev work without going through the hassle of bothering our main devs. However at the very end of the process, I run rails s command. I get landing page on localhost:3000 and can get through the pages without direct call to database. However when I go to any page that gets the data from db, it gives me following error:
ActiveRecord::StatementInvalid in WantsController#new
PG::Error: ERROR: relation "geometry_columns" does not exist
LINE 1: SELECT * FROM geometry_columns WHERE f_table_name='wants'
^
: SELECT * FROM geometry_columns WHERE f_table_name='wants'
Now, no idea what I'm doing wrong. Here are the steps I'm taking:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" >> /etc/apt/sources.list'
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.3-postgis pgadmin3 postgresql-contrib postgresql-server-dev-9.3
sudo -u postgres psql
postgres=# CREATE EXTENSION adminpack;
postgres=# CREATE EXTENSION postgis;
postgres=# \q
postgres=# CREATE USER abc WITH PASSWORD 'abc';
postgres=# CREATE DATABASE abc;
postgres=# CREATE DATABASE abc_development;
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U abc -d abc abc-20131217-09.pgdmp
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U abc -d abc_development abc_development-20131217-09.pgdmp
Then navigate to cloned git folder and execute bundle and rails s
Here's the database.yml:
development: &dev
# adapter: postgresql
database: abc_development
username: abc
password: abc
host: localhost
encoding: utf8
postgis_extension: true
schema_search_path: public,postgis
adapter: postgis
encoding: utf8
postgis_extension: postgis
production:
adapter: postgis
database: abc
username: abc
password: oTSZ1gQdwsFXWIUZsehj
host: localhost
encoding: utf8
postgis_extension: true
schema_search_path: public,postgis
postgis_extension: postgis
Any ideas where it is going wrong? Help would be much appreciated.

Okay, I solved it somehow. Don't really know what did the trick but basically, it involved giving superuser privileges to "abc" user, then running db:create, db:migrate, importing db file from our site backups and running migrations again.

The error message is telling you that those tables do not exist in your database. You need to create migrations(basically just hunks of sql that rails can generate for you) then run rake db:migrate to run the migrations on your database.
You can read more about migrations at the rails guide to migrations.

Related

AWS ElasticBeanstalk Symlink

I don't want to place my config/database.yml file in GITHUB, rather to have it somewhere in the somewhere and do the symlink like below
config/databse.yml -> /var/www/database.yml
In the .elasticbeanstalk/00packages.config, I have following stuffs
commands:
# install WKHTML
03_command:
command: yum install xz urw-fonts libXext openssl-devel libXrender
04_command:
command: wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
05_command:
command: tar -xJf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
06_command:
command: cp wkhtmltox/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
07_command:
command: cp wkhtmltox/bin/wkhtmltoimage /usr/local/bin/wkhtmltoimage
Can I add symlink stuff here and does it work
Unfortunately, none of the EB custom configurations allow you to perform operations after your application has been deployed. At the point at which commands are run, your application is still in /var/app/ondeck, so if you create a symlink under /var/app/current, it will be deleted when ondeck is renamed to current.
The only way around this limitation that I'm aware of is to put a file into the appdeploy/post hooks directory. In your case, it would look something like this:
.ebextensions/01symlink.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_create_symlink.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
ln -sf /var/www/database.yml /var/app/current/config/database.yml
Alternately, you could use a commands entry to copy the file from /var/www to /var/app/ondeck during the deploy.
I was able to create a symlink using container_commands and a relative path, which is your app's root directory by default. Although the /var/app/current directory does not exist, you can create a symlink and it will get moved to /var/app/current when the deploy completes. For example:
container_commands:
01symlink:
command: sudo -u webapp ln -sf /var/www/database.yml ./config/database.yml
An better and far simpler solution is to use the DATABASE_URL environmental variable to configure the database connection. This is how Heroku for example "magically" connects your app to the database created by its postgres addon.
For example:
postgresql://somehost/some_database?username=foo&password=bar
Just leave the bare minimum in database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
This makes it perfectly safe to check into version control.
And then set the env var through environmental properties on AWS.

Error message: Make sure that `gem install pg -v '0.18.1'` succeeds before bundling

i have a problem with ruby. I tried a lot but nothing works for me.
When i want to start the rails server, i get this error message:
An error occurred while installing pg (0.18.1), and Bundler cannot continue.
Make sure that "gem install pg -v '0.18.1" succeeds before bundling.
This is what i tried already:
sudo install gem
bundle install
bundle install --path vendor/cache
gem install pg -v '0.18.1'
When i try gem install pg -v '0.18.1'i get this error message:
Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or installed on this machine.
Run bundle install to install missing gems.
But bundle installdoesn't work either. I get this error message:
An error occurred while installing pg (0.18.1), and Bundler cannot continue.
Make sure that gem install pg -v '0.18.1' succeeds before bundling.
I also tried to start the server in a new ruby project.
Nothing helps..
Thanks for your help!
These are my changes in my Gemfile:
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
end
If you're on Ubuntu, most likely you're missing a hidden dependency
sudo apt-get install libpq-dev
If you are on OS X, try these steps
Install Xcode command line tools (Apple Developer site). If you have
it already installed, update it using brew update.
brew uninstall postgresql
brew install postgresql
gem install pg
If you are an Ubuntu user you need to do the following prior installing the gem
sudo apt-get install libpq-dev
Then perform gem install pg -v '0.18.1' or just bundle install if you have you r gem in a GEMFILE.
If you are using Mac and Homebrew, Looks like libpqxx lib was missing.
brew install libpqxx
This command should do it.
If you are not sure where your pg_config is, and assuming you are on Linux or Mac, you can run the following command:
which pg_config
this will return ==> /usr/pgsql-9.1/bin/pg_config
now use this path as
bundle config build.pg --with-pg-config=/usr/pgsql-9.1/bin/pg_config
Done now bundle install
My teacher at Bloc had the solution! If anybody have the same problem run the following commands:
spring stop
gem uninstall pg
bundle install --without production
This solved my problem.
ARCHFLAGS="-arch x86_64" bundle install worked for me as discussed here.
Bundler has some problem identifing the PostgreSQL server path. If you are pretty sure that the your PostgreSQL server is installed properly, all you need to do should be adding the path to this to PATH variable. Example command:
export PATH=/path/to/postgres/bin/:$PATH
If you would still have some issues, it is very likely that you have something wrong with PostgreSQL install. If so, try installing Postgres.app and execute command as below:
export PATH=/Applications/Postgres.app/Contents/Versions/9.3/bin/:$PATH
Make sure that the version is correct.
Anyone running in to this (a year later!) using Rails 5.1.2 I did the following after first installing and initiating Postgresql (Centos 7).
So assuming you've already got postgresql installed and the postgres server&user set up.(+ the usual standard dev tools for Linux).
Add extra deps for rails to build gems.
$ sudo yum install postgresql-devel
Add postgres path in ~/.profile
export PATH=/usr/bin/postgres:$PATH (or your installed path)
Add another user/role with create db privileges using pgAdmin or shell
(should be the same user as the system/rails user because the postgres user doesn't have permissions for /rails/db/schema.rb, but the system/rails user does)
Below are shell commands for postgres create role and database.
$ sudo -u postgres psql (enter postgres password)
$ create role (linux/rails user) with createdb login password 'password';
$ \du (check its done and has createDB privs)
$ CREATE DATABASE name;
You will automatically become owner of the new database if no other arguments are presented.
Or you can use a gui like DBeaver to do the same.
So the above sets up for rails to access postgresql and build the pg gem once you've swapped out the default Gemfile & config/database.yml
Now create app and set it up (no need for -d postgresql flag because we swap out the Gemfile and the config/database.yml file contents completely and rails will install a postgresql db on bundle update/install.
(change some to rake for earlier versions of rails)
$ rails new app
$ cd app
$ atom (or editor) Gemfile config/database.yml
Swap out both file contents (to ones shown below) & save.
$ bundle update
$ bundle install
Check it with
$ rails db:create
(postgresql database should now be connected), so scaffold something
$ rails g scaffold Users name:string email:string comment:text
$ rails db:migrate
$ rails server
http://localhost:3000 shows the default page and http://localhost:3000/users brings up your new Users page using postgresql not sqlite3. Put something in to test it.
Below are the Gemfile and config/database.yml files I used for Rails 5.1.2, including taps for Heroku.
Gemfile rails 5.1.2
source 'https://rubygems.org'
gem 'rails', '5.1.2'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.7.0'
gem 'taps'
#Postgresql Database
group :production do
gem 'pg', '0.21.0'
end
group :development, :test do
gem 'sqlite3', '1.3.13'
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.0.8'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
config/database.yml file contents (watch out for indentation)
development:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
test:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
production:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
Doing all the above will get you a dev/prod postgresql database in rails but no test, you can also access the database directly from rails by just running "rails db" and entering password.
Its simple but gets you up and running with rails/postgresql quickly.
For Alpine linux, you'd want to install postgresql-dev first:
apk add --update postgresql-dev
Try install pg like this:
gem install pg -- --with-pg-dir=/path/to/postgresql/root
If it doesn't work, try
gem install pg -- --with-pg-include=/path/to/postgresql/root/include \
--with-pg-lib=/path/to/postgresql/root/lib
PostgreSQL setup on CentOS 6
[root#git2 ~]# yum install postgresql-server
[root#git2 ~]# psql
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
[root#git2 ~]# /etc/init.d/postgresql
Usage: /etc/init.d/postgresql {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb}
[root#git2 ~]# /etc/init.d/postgresql start
/var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first.
[FAILED]
[root#git2 ~]# service postgresql initdb
Initializing database: [ OK ]
[root#git2 ~]#
[root#git2 ~]# /etc/init.d/postgresql start
Starting postgresql service: [ OK ]
[root#git2 ~]# psql
psql: FATAL: Ident authentication failed for user "root"
[root#git2 ~]#
[root#git2 ~]# su - postgres
-bash-4.1$ psql
psql (8.4.13)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privilege
s
-----------+----------+----------+-------------+-------------+-------------------
----
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postg
res
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postg
res
(3 rows)
postgres=#
postgres=# \q
-bash-4.1$
-bash-4.1$ pwd
/var/lib/pgsql
-bash-4.1$ wget -q http://www.commandprompt.com/ppbook/booktown.sql
-bash-4.1$ ls -lh booktown.sql
-rw-r--r-- 1 postgres postgres 42K Jan 11 2005 booktown.sql
-bash-4.1$ psql -f booktown.sql
CREATE DATABASE
(snip)
-bash-4.1$ psql
psql (8.4.13)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privilege
s
-----------+----------+----------+-------------+-------------+-------------------
----
booktown | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postg
res
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postg
res
(4 rows)
postgres=#
postgres=# \q
-bash-4.1$
-bash-4.1$ pg_dumpall > /tmp/pg_dumpall.`date +%s`.sql
-bash-4.1$ ls -lh /tmp/pg_dumpall.1349195444.sql
-rw-r--r-- 1 postgres postgres 44K Oct 2 12:30 /tmp/pg_dumpall.1349195444.sql
-bash-4.1$
-bash-4.1$ pg_dump --clean booktown > /tmp/pg_dump-booktown.`date +%s`.sql
-bash-4.1$ ls -lh /tmp/pg_dump-booktown.1349196164.sql
-rw-r--r-- 1 postgres postgres 46K Oct 2 12:42 /tmp/pg_dump-booktown.1349196164.sql
-bash-4.1$
-bash-4.1$ psql booktown
psql (8.4.13)
Type "help" for help.
booktown=#
booktown=# \d
(snip)
public | subjects | table | postgres
public | text_sorting | table | postgres
booktown=# SELECT * FROM subjects;
0 | Arts | Creativity St
1 | Business | Productivity Ave
(snip)
PostgreSQL setup on Fedora 20
[root#localhost ~]# yum install postgresql-server
(snip)
[root#localhost ~]# systemctl start postgresql.service
Job for postgresql.service failed. See 'systemctl status postgresql.service' and 'journalctl -xn' for details.
[root#localhost ~]#
[root#localhost ~]# systemctl status postgresql.service
postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled)
Active: failed (Result: exit-code) since Sat 2014-01-18 08:08:38 EST; 12s ago
Process: 4921 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE)
Jan 18 08:08:38 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
Jan 18 08:08:38 localhost.localdomain postgresql-check-db-dir[4921]: "/var/lib/pgsql/data" is missing or empty.
Jan 18 08:08:38 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1
Jan 18 08:08:38 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server.
Jan 18 08:08:38 localhost.localdomain systemd[1]: Unit postgresql.service entered failed state.
[root#localhost ~]#
[root#localhost ~]# postgresql-setup initdb
Initializing database ... OK
[root#localhost ~]#
[root#localhost ~]# systemctl start postgresql.service
[root#localhost ~]#
[root#localhost ~]# systemctl stop postgresql.service
[root#localhost ~]#
[root#localhost ~]# cp -a /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.orig
[root#localhost ~]# # insecure... just for dev...
[root#localhost ~]# vim /var/lib/pgsql/data/pg_hba.conf
[root#localhost ~]# diff /var/lib/pgsql/data/pg_hba.conf.orig /var/lib/pgsql/data/pg_hba.conf
80c80
< local all all peer
---
> local all all trust
82c82
< host all all 127.0.0.1/32 ident
---
> host all all 127.0.0.1/32 trust
[root#localhost ~]#
[root#localhost ~]# systemctl start postgresql.service
[root#localhost ~]#
[root#localhost ~]# su - postgres
-bash-4.2$ psql -c '\du'
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
-bash-4.2$ psql -c "CREATE ROLE pguser1 UNENCRYPTED PASSWORD 'secret1' NOSUPERUSER CREATEDB CREATEROLE NOINHERIT LOGIN"
CREATE ROLE
-bash-4.2$ psql -c '\du'
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
pguser1 | No inheritance, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
-bash-4.2$
-bash-4.2$ psql -c '\l'
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
-bash-4.2$
-bash-4.2$ psql -c "CREATE DATABASE pgdatabase1 WITH OWNER = pguser1"
CREATE DATABASE
-bash-4.2$ psql -c '\l'
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
pgdatabase1 | pguser1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
-bash-4.2$

Exporting mysql database using mysqldump including procedures

While exporting databases using mysqldump like this,
mysqldump -u mysqluser -p mysqlpassword databasename > /tmp/databasename.sql
Will this command also export stored procedures that listed using the following command,
SHOW PROCEDURE STATUS WHERE db = 'databasename';
If not, how to export mysql database using mysqldump along with its associated stored procedures from the Linux terminal? Also note that i cannot use phpMyAdmin for this purpose.
Try this.
mysqldump -u mysqluser -p mysqlpassword --routines databasename > /tmp/databasename.sql
Refer this link : http://www.ducea.com/2007/07/25/dumping-mysql-stored-procedures-functions-and-triggers/
We can use the -R flag as a substitute for --routines flag while dumping as the other answer suggest.
mysqldump -u mysqluser -p mysqlpassword -R databasename > /tmp/databasename.sql

How to backup/restore Rails db with Postgres?

I do the following on my server:
pg_dump -O -c register_production > register.sql
Then, after copying register.sql to my local environment, I try:
psql register_development < register.sql
This appears to work, but when I try to launch the Rails site locally, I get this:
PG::UndefinedTable: ERROR: relation "list_items" does not exist at character 28
How can I restore everything (including relations) from the server db to my local dev db?
I use this command to save my database:
pg_dump -F c -v -U postgres -h localhost <database_name> -f /tmp/<filename>.psql
And this to restore it:
pg_restore -c -C -F c -v -U postgres /tmp/<filename>.psql
This dumps the database in Postgres' custom format (-F c) which is compressed by default and allows for reordering of its contents. -C -c will drop the database if it exists already and then recreate it, helpful in your case. And -v specifies verbose so you can see exactly what's happening when this goes on.
Does the register_development database exist before you run the psql command? Because that form will not create it for you.
See http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE for more information.

How to import a Heroku PG dump into local machine

I'm trying to import my production Heroku database into my development machine.
My local db is PostgreSQL.
First, I'm exporting the dump from Heroku to my machine
curl -o latest.dump `heroku pgbackups:url`
Then, I try to drop the local db with rake db:drop and then I create the empty database again by using rake db:create.
The problem I'm getting is when actually trying to import the dump to the database
psql -d app_development -U myusername -f mydumpfile.sql
I begin seeing errors like this
psql:latest.dump:24: ERROR: syntax error at or near "PGDMP"
LINE 1: PGDMP
^
psql:latest.dump:28: ERROR: syntax error at or near ""
LINE 1: INCREMENT BY 1
^
psql:latest.dump:36: ERROR: syntax error at or near ""
LINE 1: id integer NOT NULL,
^
psql:latest.dump:40: ERROR: syntax error at or near ""
LINE 1: INCREMENT BY 1
^
psql:latest.dump:45: ERROR: syntax error at or near ""
LINE 1: id integer NOT NULL,
^
psql:latest.dump:49: ERROR: syntax error at or near ""
LINE 1: INCREMENT BY 1
...
psql:latest.dump:1601: invalid command \S4???(?̭?A?|c?e0<00K?A?}FϚ?????A(??~?t?I?????G(? K???l??k"?H?ȁ?ͲS?,N*?[(#??a5J??j}
psql:latest.dump:1602: invalid command \??k???|??w???h?
psql:latest.dump:1603: invalid command \=??????o?h?
psql:latest.dump:1609: invalid command \????^.?????????E???/-???+??>#?ؚE?.2)Ȯ&???? g????"7},_??]?:?f?Tr|o???)?p????h?KO?08[Rqu???|3?cW?ڮ?ahbm??H?H8??$???2?a?-أ
psql:latest.dump:1613: invalid command \D!qVS???L??*??׬R??I!???
psql:latest.dump:1614: invalid command \??-?}Q
psql:latest.dump:12565: ERROR: invalid byte sequence for encoding "UTF8": 0xb0
Any idea what is happening this and how to solve it?
You see errors because psql tries to interpret SQL queries when you're actually giving him a compressed dump (that's what heroku uses).
While you can't read the dump, pg_restore -O latest.dump gives you valid SQL you could pipe to psql but the easy solution is the following one :
pg_restore -O -d app_development latest.dump
Notes :
Use -O because you probably don't use the random username of your remote heroku postgres db.
Heroku doesn't recommend to use taps but I don't know how really risky it is.
Follow these 4 simple steps in your terminal(Heroku Dev Center):
Create a backup copy of your database:
$ heroku pg:backups capture DATABASE_NAME
Download the copy from Heroku (to your local machine) using curl:
$ curl -o latest.dump `heroku pg:backups public-url`
Load it*:
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U YOUR_USERNAME -d DATABASE_NAME latest.dump
get YOUR_USERNAME and choose the desired database from your config/database.yml file.
DATABASE_NAME can be your development/test/production db (Ex. mydb_development)
That's it!
I wanted to avoid having to set up Postgres on my local machine (blowing away and recreating the database is a pain if you're just looking for quick instructions). I put together some exact instructions for doing this with a local Postgres database running Docker. I'm adding a link here, since Google kept bringing me to this question (and it's a possible solution, though probably not what you're looking for):
https://gist.github.com/locofocos/badd43131f14b3e40c760741d5a26471
Heroku export the .dump extension file of the db to import in any of the relational DB by having its own norms and conditions.
While importing it to the local postgres DB, first you download the file latest.dump into your local machine and then run
pg_restore -h localhost -p 5432 -U postgres_username -d db_name -v latest.dump
and restart the rails server.
Late 2021 update for the highest voted answer to date (works great):
$ rails db:drop db:create db:migrate
$ heroku pg:backups capture DATABASE_URL
$ curl -o latest.dump heroku pg:backups public-url
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U YOUR_USERNAME -d DATABASE_NAME latest.dump
get YOUR_USERNAME on your local machine
DATABASE_NAME can be your development/test/production db (Ex. rails_react_bootstrap_development) from your config/database.yml file.
DATABASE_URL is not a variable or example code you need to set. It is a valid heroku option

Resources