PostgreSQL - using psql terminal to restore a dump - error: stdin is not a tty - postgresql

On Windows 10, using psql with PostgreSQL 14 installed, I'm trying to restore a dump that is my_dump.sql.
I'm on Bash terminal currently.
I'm connected to psql server and I created a new database new_db, then a new superuser tobe.
Then I tried the command psql -d new_db < my_dump.sql but I'm getting the error stdin is not a tty
I tried the solution psql -U tobe -d new_db -f my_dump.sql from "stdin is not a tty" when populating Postgres database
because they mention the error but now I get a new error:
ERROR: syntax error at or near "ÿþ" LIGNE 1 : ÿþ
Are the two errors connected? Maybe the second error is only related to the command syntax. I'm not sure if I'm in the right direction.

Try
psql.exe -U tobe -d new_db -f my_dump.sql

Related

psql faild to restor file from network drive

i have a dump file on drive z (network drive)
im opening the psql from PgAdmin4
this is the command that im writeing:
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
and this is the error that im getting:
Invalid command \DB_BU. Try \? for help.
when im doing this:
psql -U postgres -d postgres -f i\ Z:\DB_BU\md_20220729.sql
Invalid command \DB_BU. Try ? for help.
and when im doing this:
psql -U postgres -d postgres -f "Z:\DB_BU\md_20220729.sql"
im not getting any error but also its not restoring the file. how can i restor the file?
You're trying to call psql from within psql or PGAdmin. Since
psql is a standalone program, not an SQL command you can run in PGAdmin SQL window or psql's own, internal meta-command you're getting the error
Invalid command \DB_BU. Try \? for help
indicating that there was an attempt to interpret your entire command as a SQL query or an internal command and that this attempt failed.
You can open "psql tool" from within PGAdmin but your command won't work there either because it's trying to call psql itself, with some command-line options, which you cannot do when you're already inside an interactive psql session. The command
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
can be used outside psql and PGAdmin, in your terminal, like zsh on Mac, sh/bash on Linux, cmd or PowerShell on Windows, where psql is installed and visible, along with your network path.
If you're able to open the psql tool window in PGAdmin, you can instead try and use an internal psql \i meta-command which is basically the same thing as the -f command-line option, but meant for use inside the psql session:
\i "Z:\DB_BU\md_20220729.sql"

Copying local postgresql to AWS RDS

I have a Postgesql database that I want to copy/replicate from my local machine to an AWS RDS instance.
I've created a dump as follows:
pg_dump -v -U postgres mydb > mydb.dump
I then tried to copy to my RDS as follows:
psql -f mydb.dump -h XXXXXXXXXXXX.us-east-1.rds.amazonaws.com -p 5432 -U postgres -d mydb
However, I get the following error:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I then tried to rule out any issues with RDS by copying the archive to another local database as follows:
pg_restore -U postgres -d mydbcopy mydb.dump
I received an error:
pg_restore: error: input file does not appear to be a valid archive
I also tried the preceding as follows:
psql -f my.dump -U postgres -d mydbcopy
And got the same error as before:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I am using Windows Powershell and Postgresql 13.2.
Am I missing something ?
It seems to be an issue with PSQL tools in view that I'm getting the error even when trying to copy locally.
I managed to solve this problem by using
pg_dump -f dump.sql --inserts mydb
It turns out using the redirect ">" means PowerShell does the file writing and it uses UTF16 which doesn't play nice with psql. The -f flag makes pg_dump do the writing itself.

PSQL file execution

I am trying to execute a .sql file using UNIX command line for postgres 9.5. I have tried
psql -s localhost -d postgres < filename.sql
where postgres is the name of my database, as well as
psql postgres username < filename.sql
and also
psql -af filename.sql
and I get the result:
Usage: /Library/PostgreSQL/9.5/scripts/runpsql.sh [wait]
for each of these runs. The scripts did not seem to execute, but got no error messages. I am running on Mac OSX.

altering privilege in psql interactive prompt

I am new to the use of psql prompt. I ran sudo -u postgres (psql version 9.3.9)
Then I connected to my database and I ran
=> ALTER DEFAULT PRIVILEGES FOR USER some_user IN SCHEMA public GRANT SELECT ON TABLES TO other_user;
and then I got this error message
ERROR: syntax error at or near "ALTER DEFAULT PRIVILEGES FOR USER"
LINE 1: ALTER DEFAULT PRIVILEGES FOR USER ckan_default IN SCHEMA pub..
How can I deal with that ?

NSIS running pg_restore or psql commands - failing to restore DB properly

In an NSIS installer, I use the following line to restore a PostgreSQL database from a file packaged with the installer.
ExecWait '$pg_restore_path --host 127.0.0.1 --no-password --port 5432 --username "postgres" --dbname "myDatabase" --verbose $EXEDIR/myDatabase.backup' $0
The command works but pg_restore seems to incorrectly set certain postgres sequences' current values (the current values either get reset to 1 or a number lower than that of the source) resulting in collisions. It seems to be a native bug with postgres but really not sure.
I have also tried replacing pg_restore with psql like this:
ExecWait '$psql_path -f "$EXEDIR/myDatabase.sql" myDatabase'
Which does not work; the terminal pops open and closes. Or like this:
ExecWait '$psql_path myDatabase < $EXEDIR/myDatabase.sql'
Which causes the error psql: warning: extra command-line argument "<" ignored
When I run the psql command manually from the command line it works like a charm, and sequences set properly. So my question is how to get the psql command working in NSIS with the file feeded < and avoiding the error. Failing that, any insight on using pg_restore differently that could work around the sequence issue?
Thanks
The following NSIS commands seem to have solved it.
ExecWait '$createdb_path -h "127.0.0.1" -p "5432" -U "postgres" -T "template1" --owner "user_owner" myDatabase'
ExecWait '$psql_path -f "$INSTDIR/myDatabase.sql" myDatabase user_owner'