How to connect to heroku pg from local machine? - postgresql

This document says to set DATABASE_URL=postgres://$(whoami) this to maintain parity between local and pro dbs. But what should I replace "whoami" with?
I have installed PostGreSql on my machine. How to I perform CRUD operations?

That whoami will return username of the current user when invoked. As the documentation said, you just need to type the below in command prompt (no need to replace whoami), depends on your OS:
Linux / Mac:
export DATABASE_URL=postgres://$(whoami)
Windows:
set DATABASE_URL=postgres://$(whoami)
After set up Postgres, you can use psql client to access the database. As you asked in comment regarding how to run .sql file, here's the steps:
In command prompt, type "psql". You are now connected to your database.
Inside psql, type "\i your_sql_file.sql". This execute the your_sql_file.sql file.

Related

I have a question about postgreSQL cluster

I am new to programming. I used PostgreSQL because I needed a database program.
I made a data file using initdb.exe
initdb.exe -U postgres -A password -E utf8 -W -D D:\Develop\postgresql-10.17-2-windows-x64-binaries\data
This method is called a data cluster.
I have put a lot of information into this data file.
Now I want to transfer the data to another computer and use it.
How do I import and use files created using a cluster?
I want to register and use it in pgAdmin4.
What should I do?
I am using a Windows 10 operating system. A solution similar to loading a cluster is required.
As long as you want to transfer to another 64-bit Windows system, you can just shut down the server and copy the data directory. Register a service with pg_ctl register if you want.
To copy the data to a different operating system, you have to use pg_dumpall and restore with psql. pgAdmin won't help you there (it is not an administration tool, as its name would suggest).

Firebird 3 on macOS, local connection fails with: Can not access lock files directory /tmp/firebird/

I've installed firebird 3.0 from the package provided by firebirdsql.org.
If I try to use a local connection to a database:
isql employee -user SYSDBA
it fails with:
Can not access lock files directory /tmp/firebird/
So adding read/write/execute permissions to /tmp/firebird/
sudo chmod a+rwx /tmp/firebird/
and executing the command again yields:
Statement failed, SQLSTATE = 08001
I/O error during "open" operation for file "/tmp/firebird/fb_init"
-Error while trying to open file
-Unknown error: -1
This all will work if I sudo the calls, but is this really necessary?
What is the correct way to use a local connection to firebird database on macOS?
I found CORE-3871 issue in the firebird issue tracker, which describes the problem and it's solution. The user which tries to open the local connection must be member of the firebird user group.
So a user is added to the firebird group on mac bash with the following command:
sudo dseditgroup -o edit -a myusername -t user firebird
If you try to open the sample database employee, shipped with firebird, it's also necessary to grant the group write access to the employee.fdb:
sudo chmod g+w /Library/Frameworks/Firebird.framework/Resources/examples/empbuild/employee.fdb
Now /Library/Frameworks/Firebird.framework/Resources/bin/isql employee -user SYSDBA should work
I only put -p and the password and it's just fine. It's working.
You current command creates the Firebird Embedded database engine to connect to the database. To be able to do that, your current OS user needs to have sufficient access to the database file. For details how to fix that, see the answer by jonjonas68.
An alternative to solution - if you have the Firebird server running - is to connect through the Firebird server process, for example using isql localhost:employee -user sysdba -password <sysdbapassword>. Then the file permissions of the user running the Firebird server process will be applied. However, in that situation, you will need to specify a password when connecting, as passwordless authentication is only applied for Firebird Embedded connections.

Failed to load sql modules into the database cluster during PostgreSQL Installation

I have attempted to install PostgreSQL 9.4 and 8.4 multiple times and it is failing no matter what I have tried. I am attempting to install on Windows 7 SP1 x64. After each failed install I have uninstalled and deleted the installation folder to start fresh.
Each time I attempt the install I get an error pop up near the end of installation that says:
"failed to load sql modules into the database cluster".
Then another error pop up displays immediately after that says:
"Error running post install step. Installation may not complete correctly. Error reading C:/Program Files/PostgreSQL/9.4/postgresql.conf"
I have attempted installation with the following actions:
Always installed as administrator
Turned off all virus protection and windows firewall
Changed the installation directory to something other than the Program Files directory.
Changed the data directory to something other than the installation directory of postgres
None of the actions above have helped and I always receive the error. Any help that someone can provide would be greatly appreciated!
Encountered a very similar problem that OP is reporting today while installing Postgres 9.4.
It turns out that the password generator I was using has made a password that contains non-alphanumeric characters. ("^") I believe was the culprit in this case. Removing that allowed the installation of Postgres 9.4 (Windows 64 bit) to complete.
Very easy fix once you've found it, but the error message that comes up is not descriptive, so in this case I didn't notice at first what I was doing wrong.
I was getting this same error when trying to install PostgreSQL v9.4.4 on Windows 10 Pro. Starting with a solution hosted on Stack Exchange, I came up with the following steps that allowed the installer to run successfully:
1) Create a new user account, called postgres
2) Add the new account to the Administrators and Power Users groups
3) Restart the computer
NOTE: I added step #3, since step #4 didn't work without it
4) Run a command prompt as the postgres user, using the command:
runas /user:postgres cmd.exe
5) Run the installer from the postgres command window
6) Delete the postgres user account, as well as the user directory
NOTE: I added step #6, since the postgres account is not required after installation
What worked for me is, during the install, specifying a Postgre SQL data folder that's outside of any Windows user profile directory (C:\Users), such as C:\postgres-data.
My setup:
Win 10 Pro
PostgreSQL 9.5 RC1
I ran into this issue when I tried setting PostgreSQL's data directory somewhere under my user profile's directory, such us somewhere under "My Documents".
I tried Jeff G's solution and it didn't work at first. It worked only when I kept the data directory as the default (C:\Program Files\PostgreSQL\9.5\data). So then I tested further and tried setting the data directory to somewhere outside of any user profile directories (i.e. somewhere that isn't under C:\Users). For example, C:\postgres-data and this worked.
I then just tried using that directory with my default user, and not bothering with the postgres temp user as outlined in Jeff G's solution and that also worked. So in the end, it had to do with my data directory being somewhere under C:\Users. As long as it was outside of that, it worked.
None of these answers here helped me, finally I solved this problem by creating the folder before the installation (C:\PostgreSQL\data) and giving it full access for the group "Users".
Windows 7 x64, postgresql-9.5.2-1-windows-x64
Hope It's Work.
1.)Uninstall PostgreSQL
2.)Delete the postgres user if it still exists :
net user postgres /delete
3.) Create the postgres user with a password you can remember:
net user /add postgres
4.) Add the postgres user to the Administrators group:
net localgroup administrators postgres /add
5.) Add the postgres user to the Power Users group
net localgroup "power users" postgres /add
6.) Run a command window as the postgres user:
runas /user:postgres cmd.exe
7.) Change user postgres and install postgresql
8.)Back your and remove the postgres user from the Administrators group.
net localgroup administrators postgres /delete
I had the same error ("Failed to load SQL modules into the database cluster.") when installing on W2K12R2 using the EnterpriseDB installer linked to from the PostgreSQL Windows download page. I tried running the installer with admin privs, and using the postgres-user solution offered by Jeff G, but neither worked. Finally I tried the second installer, BigSQL, and that installed without issue.
When installing PostgreSQL, do not use the following symbols in your database's admin password: %, <, and >.
Bug report and problem solution here
Changed the data directory to something other than the installation directory of postgres
Make sure NETWORK SERVICE has read/write permissions on that folder, the installation was failing for me until I did this.
I had this issue too with 9.5 and got around it by:
Installing with the default data folder.
Making sure my new data folder had full control access for the "NETWORK SERVICE" account.
Then changing the default PGDATA folder as per this instruction: https://wiki.postgresql.org/wiki/Change_the_default_PGDATA_directory_on_Windows
I had the same problem, and noticed that some Postgres .bat files were opening in a text editor. Text editors opening files during installation isn't too uncommon, but I noticed these batch files were setting directory permissions. I then found that .bat files were set to open in a text editor (Notepad++) by default. I changed the default program for .bat files, uninstalled Postgres, re-ran the installer, and it worked perfectly. Hope this helps.
I had the same problem before. I solved this by installing Visual Studio C++ Express first.
I had the same experience as #gomisha. #Jeff G's solution did not work for me no matter which user ran the installer.
As long as the target data directory was anywhere under c:\users, the install or database cluster init would fail.
I was successful after creating c:\postgresql-data and ensuring that the postgres user had full access in the 'effective access' tool.
Note: I did not use control userpasswords2 to set up the postgres user; I did it through the traditional crappy Windows 10 user wizard that tries to make the user sign up for hotmail. I did run the install as postgres.
I was getting this issue with the BigSQL windows installer PostgreSQL-9.6.5-1-win64-bigsql.exe
The problem turned out to be that my system path was too long. After removing some uncessary junk from my System Path, and then reinstalling, the problem was gone.
One of the answers here is the only thing that worked on my machine:
I had the same error message when trying to install 9.2.4. My issue
was because even though the Win2k8 server had %SYSTEMROOT%\system32 as
part of the path, no programs could "see" anything in
C:\Windows\system32. The installer heavily uses icacls.exe during the
init routine. Since my path was screwed up, the installer bombed out.
Once I explicitly added C:\Windows\system32 to the SYSTEM's Path
environment variable and re-ran the installer as an admin, everything
worked fine.
Credits to the user.
Go to services.msc
Search for postgres service
Go to Log On tab
Select local account
Profit
The alternative solution for problems with PostgreSQL installer is setting this up with a Docker.
Install Docker Desktop
https://www.docker.com/products/docker-desktop/
Start installed Docker Desktop
In PowerShell, run:
docker pull postgres
In PowerShell, run:
docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=mypassword postgres
Now you can connect to the Postgres server for example with pgAdmin on the same machine, using:
host: localhost
port: 5432
maintenance database: postgres
username: postgres
password: mypassword
I think this would be better as individual comments above, but I don't have the required rep. I just spent days on this, with Postgres 10 on Win 10 Creators. My "answer" was to let Postgres put the data in its default install folder instead of my User folder (where it would be automatically backed-up).
1. This problem can happen with no reported install errors at all. On my very first try, I saw:
Failed to load SQL modules into the database cluster.
But that seemed to be due to:
Executing C:\Users\loren\AppData\Local\Temp\postgresql_installer_aee8e5a76f\vcredist_x64.exe /passive /norestart
Script exit code: 3010
MSI (s) (AC:FC) [14:21:35:341]: Product: Microsoft Visual C++ 2013 x86 Minimum Runtime - 12.0.40660.
Restart required. The installation or update for the product required a restart for all changes to take effect.
The restart was deferred to a later time.
Restarting Windows after the failure of the rest of the Postgres installation did not let Postgres run.
Once VC was in place, uninstalling and re-installing Postgres (still with its data in my User folder) completed with no reported errors. But every attempt to access it, by any means, still resulted in something like this:
C:\Program Files\PostgreSQL\10\bin>psql
psql: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The RawCap sniffer showed TCP SYN followed immediately by RST,ACK on localhost for both v4 and v6 for each try. Netstat did not see port 5432 in use at all. All Postgres runtime logs were totally empty. In Windows Services, I could manually start the postgresql-x64-10 service, but it immediately stopped itself, with the note that some services do that normally.
2. I tried moving the data directory via the Postgres wiki procedure. Apparently for Postgres 10 it is incomplete. It does not deal with:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-x64-10\Data Directory
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Services\postgresql-x64-10\Data Directory
C:\Program Files\PostgreSQL\10\pg_env.bat
C:\Program Files\PostgreSQL\10\scripts\serverctl.vbs
But fixing every data file instance I could find did not let it run. And even after fixing all those, and uninstalling and re-installing again with the default location, the new pgAdmin4 somehow still remembered my original User data directory location!
3. I did not try giving NETWORK SERVICE read/write permissions on my User folder. Once the default install worked I wanted to get on with my original project... Maybe that would have worked.

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.

Remote trigger a postgres database backup

I would like to backup my production database before and after running a database migration from my deploy server (not the database server) I've got a Postgresql 8.4 server sitting on a CentOS 5 machine. The website accessing the database is on a Windows 2008 server running an MVC.Net application, it checks out changes in the source code, compiles the project, runs any DB Changes, then deploys to IIS.
I have the DB server set up to do a crontab job backup for daily backups, but I also want a way of calling a backup from the deploy server during the deploy process. From what I can figure out, there isn't a way to tell the database from a client connection to back itself up. If I call pg_dump from the web server as part of the deploy script it will create the backup on the web server (not desirable). I've looked at the COPY command, and it probably won't give me what I want. MS SQLServer lets you call the BACKUP command from within a DB Connection which will put the backups on the database machine.
I found this post about MySQL, and that it's not a supported feature in MySQL. Is Postgres the same? Remote backup of MySQL database
What would be the best way to accomplish this? I thought about creating a small application that makes an SSH connection to the DB Server, then calls pg_dump? This would mean I'm storing SSH connection information on the server, which I'd really rather not do if possible.
Create a database user pgbackup and assign him read-only privileges to all your database tables.
Setup a new OS user pgbackup on CentOS server with a /bin/bash shell.
Login as pgbackup and create a pair of ssh authentication keys without passphrase, and allow this user to login using generated private key:
su - pgbackup
ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ""
cp -a ~/.ssh/.id_rsa.pub ~/.ssh/authorized_keys
Create a file ~pgbackup/.bash_profile:
exec pg_dump databasename --file=`date +databasename-%F-%H-%M-%S-%N.sql`
Setup your script on Windows to connect using ssh and authorize using primary key. It will not be able to do anything besides creating a database backup, so it would be reasonably safe.
I think this could be possible if you create a trigger that uses the PostgreSQL module dblink to make a remote database connection from within PL/pgSQL.
I'm not sure what you mean but I think you can just use pg_dump from your Windows computer:
pg_dump --host=centos-server-name > backup.sql
You'd need to install Windows version of PostgreSQL there, so pg_dump.exe would be installed, but you don't need to start PostgreSQL service or even create a tablespace there.
Hi Mike you are correct,
Using the pg_dump we can save the backup only on the local system. In our case we have created a script on the db server for taking the base backup. We have created a expect script on another server which run the script on database server.
All our servers are linux servers , we have done this using the shell script.