PSQL -f Parameter Hanging on Windows 8.1 - postgresql

I've been using PSQL 14 on my Windows 10 desktop with Git Bash for a while now without issue. Recently I've had to transition to a Windows 8.1 laptop, and I've come across a problem with running the filename parameter for PSQL. When attempting to run a SQL file with the line psql.exe -U <user> -f src/sql/test.sql the terminal hangs until I use Ctrl+C to exit the command. I can run psql -U <user> and then copy & paste the SQL file text into the terminal to get the results I want, but I don't get why this issue is happening in the first place.
I've checked my PATH environment variables and I do have both the /bin and /lib paths in there. I have also tested changing -f with the < operator, which didn't change anything. Running PSQL on Windows 8.1 isn't an issue, it's just this particular command.

Related

pg_dump windows command prompt invalid command

trying to using pg_dump to backup a postgres db
i connected through the command prompt and here is my command following this tutorial http://www.postgresqltutorial.com/postgresql-backup-database/
pg_dump -U postgres -W -F t lucz_2017 > X:\postgres_backup\lucz_backup.tar
it gives me an error
Invalid command \postgres_backup. Try \? for help.
what am I doing wrong?
the db name and paths are correct
windows 7 running this from the CMD
You are running pg_dump from psql. Get out of psql and run pg_dump command from Windows Command prompt. pg_dump is its own executable, different from psql.
This works for me in Windows PowerShell in Windows 10:
.\pg_dump.exe --username "yourUserName" --no-owner "yourDatabasName" >./filename.sql
To backup my "DVD_RENTAL_DB" database to a local folder on my computer I had to use the below in the Windows command prompt while running it as an administrator:
Don't use shell redirection (>) on Windows with pg_dump. The shell will helpfully "correct" encoding issues and corrupt your dump.
Instead, specify the output filename with the -f option:
"C:\Program Files\PostgreSQL\14\bin\pg_dump" -U postgres -p 5432 -W -F p -h localhost -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql DVD_RENTAL_DB
This worked for me ONLY after I put double quotes around the pg_dump executable file path, before when I was adding the file path without double quotes the back up was not working; probably due to spaces in my file path. The PostgreSQL documentation didn't mention anything about double quotes around the pg_dump executable file path.
To Restore my Database I used the following in the Windows command prompt while running it as an administrator:
1. Open the Windows Command Prompt as an Administrator and you will be in this directory:
C:\Windows\System32>
2. Then type the following:
cd C:\Program Files\PostgreSQL\14\bin\
3. Then you'll be here in this directory:
C:\Program Files\PostgreSQL\14\bin>
4. Type the following:
psql -U postgres -d DVD_RENTAL_DB -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql
5. You'll be prompted for your password, then your database will be restored.
Steps to using pg_dump on windows
Access cmd as Admin and type
cd path_to_pg_dump PRESS ENTER
pg_dump --username your_user_name
--table=table_name --data-only --column-inserts your_database > my_table_data.sql
PRESS ENTER

How to print current working directory in psql console?

I am trying to restore a schema on a remote server using the psql console in pgAdmin and an sql dump file. I receive the following error:
user=> \i file.sql
file.sql: No such file or directory
I can't seem to print the directory listings due to lacking superuser privileges.
Is there a way to identify or print the current working directory in psql console? What is the default directory?
Typing in the command \! pwd will print the current working directory in psql.
To change working directory use command \cd <path>; replace <path> with desired path. Eg. Running command \cd /run/mount will change the current working directory to /run/mount. Try changing the your working directory to that containing the file which you want to run and then use \i meta command as you did earlier. It will definitely work.
I am shocked that no one answered the question directly in over 2 years. Everyone assumes that because you are using postgres that you must be running Linux. Well, postgres is now becoming a very popular DBMS on Windows 10.
In Windows 10, in a psql command prompt type "! dir" to print the current working directory.
To change directories in the Windows 10 psql client, "\cd /users/yourlogin".
The psql client is a unix shell running on Windows, so it is a mix of unix and dos syntax commands.

Running PostgreSQL commands from anywhere on the terminal

First off, let me say that I'm new to both using Mac and PostgreSQL. I just installed Postgres using their installer and it was installed in /Library/Postgres/... when I tried running createdb from the terminal it returned an error createdb: command not found. I ended up using /library/postgresql/9.6/bin/createdb before I coud get it to work.
Here's my question, how do I set it so that I don't have to type in the full path again to use the createdb command.
I'd love a detailed explanation.
Thanks
First you need to execute the psql command to get into the postgresql interacive shell.
In your terminal:
psql
Postgresql interactive shell should start. In this shell
> createdb yourdatabasename;
Btw: If psql is not found you will probably need to add it to your path and restart your terminal, something like this with the path matching your machine:
export PATH=/Library/PostgreSQL/9.5/bin:$PATH

Docker-machine ssh displays junk characters on windows 7

I have installed docker-machine on 64 bit Windows 7 machine. It also installed Oracle Virtual Box to which docker-machine connects to. The issue is that it is getting very difficult to work on docker-machine. Reason is ssh-ing to virtual machine displays a lot of junk characters. Below is what I get when I just vi newfile. Similar junk characters on cat existingfile. Or if I click on backspace to delete any character on command prompt.
I was having the same issue. I came across this page during my search which prompted me to try using bash.
Install git if you do not already have it. You can then run the following command from PowerShell to drop into a bash shell (assuming default location for git).
& "C:\Program Files\Git\bin\bash.exe"
I have created the alias 'bash' for this in my PowerShell profile folder, which you can find from here.
New-Alias bash "C:\Program Files\Git\bin\bash.exe"
Now drop into a bash shell first before using docker-machine ssh and there should be no more junk characters.

MongoDB restore works on CLI but not inside BASH

I don't know why a command that runs OK in the command line, gives an error inside a bash script.
mongorestore -h XXXX.mongohq.com:10025 -u USER -p PASS -db pr4
DIC24/pr4
This works OK on the CLI, but inside a bash script I get:
Mon Dec 24 19:48:52 ERROR: don't know what to do with file [DIC24/pr4]
I found that the BASH changed the working dir in another line (I was editing a bash file created by other) so the script was running in another directory.
Hard to say without knowing all the details of your connection screen, but watch out for special bash characters. When automating some console commands I ran via Bash Scripting I ran into issues because one of the credentials contained an exclamation point.
Another thought: are you executing the script from the directory it's stored in?