Heroku pg:pull is giving sh: createdb: command not found - postgresql

When I try to do heroku pg:pull DATABASE_URL myappspassword, I get this error:
my-computer:a-folder (master*) · heroku pg:pull DATABASE_URL myappspassword
! sh: createdb: command not found
!
! Unable to create new local database. Ensure your local Postgres is working and try again.
For once Googling doesn't return a result. I'm wondering if it's related to the fact that when I do which psql, there is no result. Maybe I need to do something special with pgAdmin to get this working (e.g. export command line tools)?

Are you on a Mac, by chance, and using Postgres App?
If so, the problem might be that createdb isn't on your path. Try adding it by inserting the following into ~/.bash_profile.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
Then run source ~/.bash_profile and try again.
There's more info on the Postgres App site: http://postgresapp.com/documentation/cli-tools.html

Your instincts are correct. You need to be able to access the Postgres from your command line. pg:pull is attempting to create a new local database and drop the relevant data from your Heroku database into it. From the docs:
This command will create a new local database named “mylocaldb” and then pull data from database at DATABASE_URL...
Basically, your local machine is trying to run createdb locally but your terminal isn't recognizing it.

I battled this damn error for a few hours. I can't say for certain if this will work for everyone but I found my issue stemmed from running the command via iTerm/ZSH instead of just normal bash. I opened up plain-old vanilla terminal, ran the command, and everything fired off like it should.

Related

Error while using createdb command postgresql on windows 10

I am a new user of PostgreSQL, and I am learning it so that I can use it with Django, so I am trying to use the createdb command to create a new database, but even though I am entering the correct password for my account, I am getting this error. I reinstalled everything and checked everything I could think of but I was not able to solve this error. So, if I can get some help regarding this issue it would be nice.
Even using the command psql, and submitting the correct password, gives the same error.
I am using Windows 10.
As far as I checked, I needed to enter the password I used while installing PostgreSQL.
By the way, I am using the latest version of PostgreSQL 14.1
The command I used:
createdb testdatabase
createdb: error: connection to server at "localhost" (::1), port 5432 failed:
FATAL: password authentication failed for user "<username_placeholder>"
So, basically, I figured the solution myself. I am just posting it here because mostly answers are available for Linux and not Windows. So, if a windows user has a similar problem, maybe this answer could help them.
So, the first thing is, if you need to open psql, use the command:
psql -U postgres
and then enter the password you used while installing PostgreSQL. Now, if you wish to do something similar to what I tried, what I mean is to use createdb command in the terminal itself, then you will have to create a new user using the same username as you do for your PC, like in my case, it is aryan.
(For example: C:\Users\aryan\).
I followed instructions from this website.
I personally used pgAdmin 4 to do it, you could also use the SQL commands themselves.
After doing everything, when I used the createdb command directly from the terminal/powershell, it asked my the password which I had used to create the other user( with the same username as my system/pc) using pgAdmin 4. That's it. This helped me out.

Error while using pg:push to a Heroku PostgreSQL database

I'm trying to push a local PostgreSQL database to one I've created on a Heroku app. To set up the Heroku database, I ran heroku addons:create heroku-postgresql:hobby-dev -a jasons-react-jobly-app, where jasons-react-jobly-app is the name of my Heroku app.
Then I ran heroku pg:push jobly DATABASE_URL -a jasons-react-jobly-app, where jobly is the name of my local database.
This throws the below error:
heroku-cli: Pushing jobly ---> postgresql-animate-30221
case
------------------------------------
0.9992547478473790.999254747847379
(1 row)
! Cannot read property 'includes' of undefined
The error looks like an error that JavaScript throws when it tries to read properties of an undefined object or something, but I'm not sure where that would be happening in this database push. The app runs completely fine on my local machine with no errors. There is no case column in any of my database tables, so I'm not sure where that number is coming from either.
Also, if I check my Heroku config variables with heroku config, I see:
DATABASE_URL: postgres://iqawqkjfiybndd:c3e921131c239ccd6c880ad4b601deeaa4558339a90ebedc562a5575c9099f42#ec2-54-156-85-145.compute-1.amazonaws.com:5432/dul2u6r13aq5i
So I know the database exists.
Any help with solving this error?
You need to fetch the name of the DB that you see attached to the heroku app on heroku dashboard.
At this url https://dashboard.heroku.com/apps/{put-your-app-name-here}/resources
To get all app names in heroku cli do heroku apps and you will know which app's DB name you want from its dashboard.
Once you have the name of the DB (it will look like this "postgresql-pointy-81624")
use that name in the command to push local db to that db on heroku .
Here are steps to make sure everything works correctly
1- Check the connectivity to the heroku db first by doing
heroku pg:psql postgresql-[somedb name as found above] --app {your-app-name}
2- If it says to reset the DB and you can afford to reset then reset it using
heroku pg:reset -a {your-app-name}
3- Lastly do the migration of DB using this command
heroku pg:push jobly postgresql-[db name found above] -a {your-app-name}
if everything works as expected you will see this message
heroku-cli: Pushing complete.

Database transfer from Heroku to Digital Ocean

I'm currently in the process of switching my cloud server from Heroku to Digital Ocean. However is there a way to migrate the database from the heroku server to the digital ocean one? I use postgresql for my database
I hope you already got a solution, but in case you didn’t, I’ll provide a simple guide on how I did it. I am going to assume that you have already created a postgres database on digitalocean. Also you need navigate to your project directory and log in to heroku using the heroku cli. And, you need to have postgresql installed or a psql client. Installing postgresql would do it as it comes with psql.
Step 1: Create a backup and download the backup from heroku postgres
heroku pg:backups:capture --app <app_name>
heroku pg:backups:download --app <app_name>
The first command will create a backup of your database and the second command will download it to your current directory, its a .dump file. If you would like to read more, here is an article.
Step 2: Connect to your remote (digital ocean’s) database using psql
Before you can do this, you need to go and add your machine you are connecting from to the list of database’s list of trusted sources, If you don’t, you’ll get a Connection Timed Out error. That’s because the database’s firewall doesn’t allow you to connect to the database from your local machine or resource (for security reasons).
Step 3: Import the Database
pg_restore -d "postgresql://<database_username>:<database_password>#<host>:<port>/<database>?sslmode=require" --jobs 4 -c "/path/to/dump_file.dump"
This will import your database from your dump file. Just substitute the variables will your connection parameters that you get from your dashboard. If you would like to read more, here is another article for this step.
Another thing to make clear is, sometimes, you will see some harmless error messages when running this command, but it will push through anyway. To learn more about pg_restore read this article.
And that’s it, your database has been migrated. Now, can you confirm it worked?, well, as for me, I used pgAdmin to connect to the remote database and I saw the tables and data as expected.
Hope this helps anyone with the same problem :)

Heroku database restore issue

Have gone through different solutions available on stackoverflowand also on different forums. But none addresses the precise problem.
As per the documentation: https://devcenter.heroku.com/articles/heroku-postgres-import-export
I have the dump file created from my local database, with this command:
pg_dump -Fc --no-acl --no-owner -h localhost -U postgres dss_iaya>dss_iaya_db_dump1.dump
Then as per documentation, uploaded to a server with public access URL: https://firebasestorage.googleapis.com/v0/b/iaya-664f3.appspot.com/o/dss_iaya_db_dump1.dump?alt=media&token=06167d04-1e98-4e4b-b0e0-9d83a86dd167
Now when I try to restore on Heroku as per its documentation syntax heroku pg:backups:restore [BACKUP] [DATABASE] --app APP using following command, it returns error message when restoring.
heroku pg:backups:restore --app heroku-postgres-*** 'https://firebasestorage.***/dss_iaya_db_dump1.dump?alt=media&token=***' 'postgres://quesu***:I***#ec2-54-***.eu-west-1.compute.amazonaws.com:5432/d3n***k0'
I have used *** for security purpose only, as can not mention full credentials. But I believe one can understand the whole syntax.
When I restore same .dump file on a newly created local database it works without any issues and creates/restores the whole database with tables and data.
Just found the solution, actually two things were wrong in my case.
One, the uploaded .dump file was not well readable/usable by the heroku.
Two, the heroku postgresql DB complete URL was not required to be provided.
So, the right way that worked for me was that the uploaded file should be accessible without any token and also without any virtual/indirect path, etc. The URL to the file should point to the file directly. In my questioned problem, I was using firebase to host my DB file temporarily to do the heroku operation. And firebase was not giving direct URL to the uploaded physical file.
heroku pg:backups:restore --app heroku-postgres-f3*** 'https://www.h***.com/dss_iaya_db_dump2.dump' DATABASE_URL
After typing this command, I was asked to retype the heroku app name just to confirm the operation. Once done, everything worked like a charm.
In logs you see dump size: 0 Bytes. Also you see aborting and 403,
so if you check your file: https://llfirebasestorage.googleapis.com/vo/b/iaya-664f3.appspot.com/0/dss_iaya_db_dump1.dump?a1t=media
you get:
404. That’s an error.
The requested URL /vo/b/iaya-664f3.appspot.com/0/dss_iaya_db_dump1.dump was not found on
this server. That’s all we know.

heroku pg:pull password authentication failed

I'm trying to run the heroku pg:pull command, but I can't seem to get the amazingly cryptic authentication process.
The command I'm running:
> heroku pg:pull app_name::RED localdb
I then get a password prompt, which I can't, for the life of me, figure out. After 2 guesses I get password authentication failed for user "Hanan", and that's it.
I tried Heroku's password, my Windows account password, every password I use, but nothing happens. I checked, and "Hanan" is not a role in Postgresql, so trying to change the password through psql doesn't work. I have no problem logging in to Postgresql through other roles, but it's this 'default' log-in process which I can't seem to crack.
Also, since I'm using windows, I'm not sure how to run commands like sudo -u postgres psql, which I see as a possible solution.
Will appreciate any help regarding this issue, I'm really frustrated by now...
Apparently it's possible to set the environment variables PGUSER and PGPASSWORD, as described here.
However, this won't work on windows in the given syntax. To do this on windows run the following:
SET PGUSER=[pg_username]
SET PGPASSWORD=[pg_password]
after entering these two lines Postgres will log you in with the given authentication info, instead of trying to sign in with the windows username
I've run into this problem a lot when running heroku pg:pull. The issue in my case was that the pg:pull command only works if my local PostgreSQL server has a password set.
To set a password, run psql localdb and execute this SQL:
ALTER USER my_user_name with password 'my_new_password';
(You won't necessarily be required to use this password all the time. Run psql localdb and see whether you're prompted; in my case, I can still log in to psql without the password.)
Now run heroku pg:pull --app my_heroku_app POSTGRESQL_COLOR localdb, and enter your new password (twice) when prompted.
I'm using Windows 10, 64-bit, Powershell and had to use the following commands to properly set the local PostgreSQL environment variables:
C:\> $Env:PGUSER="[pg_username]"
C:\> $Env:PGPASSWORD="[pg_password]"
To verify that these are set properly, list all local environment variables with this:
C:\> Get-ChildItem Env:
After doing this, I was able to run heroku pg:pull without being prompted for a password.
The previous answers did not work for me or were not to my liking so I kept searching. Thanks to the answer provided by Rayz on this post How to add a user to PostgreSQL in Windows? I was able to come up with this one liner for windows powershell.
& { $env:PGUSER="username";$env:PGPASSWORD="password"; heroku pg:push local-db DATABASE_URL --app heroku-app}
You apparently have to pass the variables as a list seperated by semicolons, surrounded by braces which are preceeded by an ampersand. Your funtion (heroku pg:pull/ pg:push/...) has to be a member of the list. As of my current testing it works in powershell with pg:push and the order of items within the braces does not matter.
I was too lazy to find out how to change credentials used by heroku. SET PGUSER and SET PASSWORD did not work for me, what i did was this:
Error said invalid credentials for "janbr" so i have created the user in the local db with a lot of priviledges. I used DBeaver for that with postgres credentials i have set up upon instalation of postgres.
Not a very clean solution though.
Using Windows 10, I tried all of the solutions here without any luck.
What ended up working for me was going into System Properties and editing my Environment Variables there. I added these to my System Variables, restarted my terminal and was able to run after that. One thing to note is that I first tried to add user and password to my User Variables but that didn't work.
PGUSER YourPostgresUser
PGPASSWORD YourPassword
I also ran Get-ChildItem Env: after updating my variables to check that they had been added.