PSQL file execution - postgresql

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.

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"

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

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

start postgres server and psql via batch

I would like to execute the following commands on Windows by batch instead of manually (cmd):
K:
cd db\pgsql\bin
pg_ctl -D "K:\db\pgsql\data" start
psql -U postgres -W -d mydb
If I just copy it to a .bat, it is starting the server and connect via psql, but I can´t execute \q to exit psql and stop the server. Is there a better way?

copy (pg_dump) .sql file to postgresql using Cygwin

I have downloaded YELP's sql file and tried to import the sql to my local Postgresql.
I am running postgres on Windows 10 and using Cygwin to execute command that I have found on Google. (it took me forever to use Cygwin instead of windows psql shell)
anyhow, Yelp gives data schema in sql and also data in sql. You may find them a link attached below
https://www.yelp.com/dataset/download
so, basically what I thought was creating an empty table with YELP's schema
the, copy all YELP's data into that table.
pg_dump -h localhost -p 5433 -U postgres -s mydb < D:/YELP/yelp_schema.sql
pg_dump -h localhost -p 5433 -U postgres -d mydb < D:/YELP/yelp_sql-2.tar
and I am checking my database transaction and it doesn't change nothing and i do not see the table.
this is what i see on Cygwin terminal
enter image description here
and nothing on my postgresql database
Please let me know what I have missed.
Thanks a lot
your link asks for email to download. I would suspect yelp to be not transparent for it. So I did not check if they have any advise on their data sets... Anyway you use pg_dump to dump the data. to import the resulting file use psql for plain sql files and pg_restore for custom format ones...
Eg:
psql -h localhost -p 5433 -U postgres -f D:/YELP/yelp_schema.sql
pg_restore -h localhost -p 5433 -U postgres -s worldmap -Ft D:/YELP/yelp_schema.sql
https://www.postgresql.org/docs/current/static/app-pgdump.html
pg_dump — extract a PostgreSQL database into a script file or other
archive file

How to run a .sql file with phppgadmin or psql

While importing .sql file in phppgadmin it produces an error
"Import error: Failed to automatically determine the file format".
How to import the file?
And in terminal it shows as
postgres-# \i Documents/sample.sql
Documents/sample.sql: No such file or directory
But the file exists in the particular location. How to import .sql file in Postgresql and in Phppgadmin
You can do this by command line:
Get login with your psql through command line and
just run the following command in your terminal
psql 'databasename' < 'database_dump'
just replace 'databasename' with your database and 'database_dump' with the file you want to import.
You can run a file using below command
psql -U postgres -d postgres -a -f ./test.sql
or if you have to pass a host
psql -h -p 5432 -U postgres -d -a -f ./test2.sql
You can refer this blog https://trueway-blog.com/run-sql-file-with-psql/