How to enable postgres on heroku for laravel 5.1 - laravel-5.1

I am not saying this hasn't been asked before, but i am unable to find the answer to this question. So here goes my question. Hope someone can guide me to the right path.
My .env file on Heroku looks something like this.
DB_HOST=postgres://somevalue.compute-1.amazonaws.com:5432
DB_DATABASE=somevalue
DB_USERNAME=secret username
DB_PASSWORD=secret password
When i run php artisan migrate, following is the error.
SQLSTATE[08006] [7] could not translate host name "postgres://somevalue.compute-1.amazonaws.com:5432" to address: Name or service not known

Remove postgres:// and the port number from DB_HOST, it should be like this
DB_HOST=somevalue.compute-1.amazonaws.com

Related

PostgreSQL error: could not connect to database template1: could not connect to server:

I'm running into the following error after uninstalling and installing several times I just can't seem to get postgresql working. I am trying to setup the coding environment for ruby on rails 6 on windows 10. I have followed the following articles and have made it all the way to creating a user where i get the following errors.
links to how i got here
https://github.com/serivas/wsl2_rails_setup
https://gorails.com/setup/windows/10#ruby-rbenv
https://www.postgresql.org/download/linux/ubuntu/
Error I'm getting when i type
sudo -u postgres createuser abc -s
Error Message;
createuser: error: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Thanks in advance!
Here's how I solved my problem for anyone that might be having similar issues.
Make sure that your windows 10 is updated to be able to run WSL2
First follow the link below to get Ruby and Rails installed.
https://gorails.com/setup/windows/10
Setup PostgreSQL from the link above,
the link below also help me setup PostgreSQL;
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database
If your having trouble logging into Postgres make sure that you create a user in Postgres that allows the user to be able to CREATEDB. So if your user is JohnDoe make sure that you give permission for JohnDoe to create database.
Links to help you out with granting permission.
https://dba.stackexchange.com/questions/33285/granting-a-user-account-permission-to-create-databases-in-postgresql
Make sure that you are starting your postgresql server.
sudo service postgresql start
And to Stop
sudo service postgresql stop
After User has rights to create the database, then you can go ahead and type in
rake db:create

Following tutorial for installing Ruby on Rails for Heroku. Currently stuck

very new to all this. I am following a tutorial to get Heroku running through ruby on rails.
I have Microsoft Windows so I am currently following Heroku's official tutorial and am stuck here
I installed PostgresSQL 10.1 using windows installer x86-64 and have noted my login information for Postgres.
However, I do not understand this part:
Remember to update your PATH environment variable to add the bin
directory of your Postgres installation. The directory will be similar
to this: C:\Program Files\PostgreSQL\<VERSION>\bin. If you forget to
update your PATH, commands like heroku pg:psql won’t work.
The closest thing I could find to an explanation and tutorial was this Java site.
I went inside Enviromental Variables and set the system path to: C:\Program Files\PostgreSQL\10\bin
However, when I tried testing it out by typing in heroku pg:psql, it said no app specified so I'm unsure if I did it correctly or not.
Also, the next tutorial step is:
Open the config\database.yml file and set the username and password
for your local (development) database. Look for these line:
I searched and I have no such file on my computers. Needless to say, I'm stumped. Any assistance to progress through the tutorial is appreciated. Thank you.
regarding your first error, the problem is that you are not specifying for which app you want to open the database cli (psql). Hopefully, the solution is easy:
heroku pg:psql --app app_name
# or
heroku pg:psql -a app_name
About the second question, the tutorial assumes that you have already created an application.
If you go to the root path of your project, you will find some subfolders such as app, config, db, lib, public, tmp, vendor.. Here you will find config\database.yml.

Postgresql PG::ConnectionBad + working with Github project

I am new to Ruby on rails. Recently I am working on a project done halfway by another programmer. What I did was I cloned his repository and installed all the required bundles and gems etc so that I can run development locally. It used to work all the time when I run rails server and visit localhost:3000
However, today I set up a dual boot Ubuntu alongside windows 8.1. Then, I followed the exact same process as before to install required gems and set up the environment.
However, this time, when I run rails server and visit localhost:3000
I run into the following problem:
could not connect to server: Connection timed out Is the server running on host "xx.xx.xx.xxx" and accepting TCP/IP connections on port 6543?
This is weird, because it used to work without me configuring anything on the database.yml.
If I start a new rails project and do
rails new projectName -d postgresql
it works fine after I edited the database.yml file to the correct credential.
So my question is, if I cloned a repository and started to collaborate with another programmer on the project, how should I set up the postgresql database for me?
Thank you very much.
If you want to make possible for everyone to have their own local database configuration, consider to create database.yml.example file. Some people think that it's a bad practice, from my experience, it's very handy.
How to make it:
Rename database.yml to database.yml.example.
Commit it
Add database.yml to .gitignore and commit this change.
Make a copy of database.yml.example and rename it back into database.yml.
Now when someone will clone your repo, she will have to create her own database.yml file from .example. So everyone will have his own database configuration.
Ok, I found the problem. It is the host address, I was using the wrong host address because my co-programmer changed the address. So, ya, if the host is correct in the database, then you should be able to see the page

rake db:create fails, authentication problem with postgresql 8.4

first things first, please excuse my utter noobness. I really tried to find a solution out there, but now i'm stuck and completely clueless.
i'm trying to deploy a rails 3 app on a distant server ; when developping on my local VM, no problem showed. But now, when i try to run
rake db:create
it fails, with error (here translated, since i'm french):
FATAL : password authentication failed for user <<mylogin>>
here's my database.yml :
login: &login
adapter: postgresql
username: mylogin
password: mypassword
host: localhost
port: 5432
encoding: UTF8
development:
<<: *login
database: somesite_development
test:
<<: *login
database: somesite_test
production:
<<: *login
database: somesite_production
the user "mylogin" has been created postgre-side with the command-line tool "createuser". It's authorized to create dbs.
postgresql.conf configures the server to listen on localhost.
I've tried many things with pg_hba.conf, none worked - whatever the method used (ident, password, md5) for user "mylogin" on 127.0.0.1, authentication fails - though i've never had problems connecting / creating dbs with psql.
any clue ?
EDIT: okay, found out how incredibly stupid i've been... the password for my user was simply not set !
I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I was also stuck on this problem for a really long time, and went to a variety of links (including the ones offered in this post) to try and find the answer, but to no avail. However, the solution is very simple. While many of the other responses were on the right track here are the exact steps to solve the problem:
Open your pg_hba.conf file in a text editor of your choice. (It is located in /etc/postgresql//main)
Navigate to the line that reads:
# "local" is for Unix domain socket connections only
and paste below it:
local all all trust
This will trust all unix users trying to connect to the psql server on the local machine. (Read documentation at top of page for further info about function of columns)
Save the pg_hba.conf file and exit the text editor.
Restart the Postgresql server by running the command:
service postgresql restart
Now try and start a psql server by running:
psql -d -U (or "psql " for short)
You should be able log in with no problem.
*Note:
All this assumes that you have have a valid psql username for logging in. If you don't follow the links below to set one up:
Setting up a user:
http://erikonrails.snowedin.net/?p=274
Making sure you have a valid postgres user:
http://archives.postgresql.org/pgsql-novice/2002-08/msg00072.php
Listing all existing psql users:
If you are looking for a "LIST USERS" or "DISPLAY USERS" command then try:
"select * from pg_user;" (when logged in to psql)
Good luck!
okay, found out how incredibly stupid i've been... the password for my user was simply not set ! I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I had same problem. In my case it was because in my database.yml file the username started with capital letter but in database it was all lower case.
Solution: When creating user from postgres command line, it converts all letters to lowercase. For using capital letters, double quotes must be used.
Example:
create user AlbertEinstein; result = alberteinstein
vs
create user "AlbertEinstein"; result = AlbertEinstein
Here are some concise instructions that should work for you
http://www.cyberciti.biz/faq/psql-fatal-ident-authentication-failed-for-user/.
Basically you need to set the authentication method for localhost to 'trust'.
You need to give "myuser" in postgresql the privilege "Can create database objects".
In pgadmin, it looks like this:
After calling rake db:create, you can take away this privilege.
In my case, I found that a later host permissions rule line in the pg_hba.conf file had over-ridden the earlier local line. I was correctly configuring the local line to use md5 authentication but the host line was set to ident which I changed to md5
As other have emphasized here, note that using trust is an insecure method, so you shouldn't use it in any production-style deployment.

Remote mysql database on Heroku app

Can I use mysql database from my personal web server instead of heroku's database?
I configured my production database like this:
production:
adapter: mysql2
database: somedatabase
username: someusername
password: somepassword
host: 1.1.1.1:1234
But, this doesn't work, my app still uses heroku's shared database.
This is old but in case anyone drops around looking for an answer, it's much easier than using the gem. Just provide a DATABASE_URL and SHARED_DATABASE_URL (not sure if the second is needed). The database url format is adapter://username:password#hostname:port/database, so for example, you would do:
heroku config:add DATABASE_URL=mysql://etok:somepassword#<your-server>:3306/etok
heroku config:add SHARED_DATABASE_URL=mysql://etok:somepassword#79.101.41.213:3306/etok
Then re-deploy your app. It will read your DATABASE_URL and generate the database.yml from that. The default port is already 3306 so it's not needed in the url in your case. When you deploy, you may notice that it generates your database.yml:
-----> Writing config/database.yml to read from DATABASE_URL
Then you're set (as long as your server accepts connections from your heroku host.
I've written a gem that may help with this. You can find it at:
http://github.com/nbudin/heroku_external_db
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
heroku config:add SHARED_DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
Then, do a
Heroku restart
that should do.
Important Note: I suggest you to use database host IP address than using giving the hostname directly, coz, with some shared hosting services like godaddy, the db hostname looks like user.345432.abcd.godaddy.com and it seems like heroku is unable to resolve it properly (personal experience), I resolved the hostname to IP address and using the IP directly worked like a charm ! Also, If your database password has special characters, make sure you escape them correctly (like '\!' for '!' and so on..)
Heroku ignores your database.yml. You will need to explore the Amazon RDS solution John Beynon suggested or some other similar addon (if there is one). IMO, you will either have to re-evaluate your need to use your MySQL db or find some other hosting.
Just in case you didn't already know it, the command:
heroku db:push
will duplicate both the schema AND data of your MySQL development database in heroku's Postgres database. So sticking with MySQL for dev is no problem.
I hope that helps.
have a look at Heroku Amazon RDS addon. I'm not saying use it, but it gives you an insight into what you need to do and how Heroku manages dataabases - basically you need to set a config variable to your mysql instance.
Yeah this is very straight forward and simple:
1 - create mysql db
2 - create mysql db user (set defaults)
3.1 - Go to your Heroku panel/Config Vars
3.2 - Click on "Reveal Vars" and edit (clicking on pencil icon) on the one you want to change in this case DATABASE_URL (if not present just a new one with DATABASE_URL as the name)
3 (#2) - Using command line
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:databaseserverport/databasename
then just
heroku restart
And remember the syntax:
DATABASE_URL
mysql://user:password#hostnameOrIPAddress:PortNumber/databasename
MySQL DBMS's default port number is : 3306
That's why you see examples mentioned previously using DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
Hope this helps!!!

Resources