I have some .sql files with thousands of INSERT statements in them and need to run these inserts on my PostgreSQL database in order to add them to a table. The files are tha
Use this to execute *.sql files when the PostgreSQL server is located in a difference place:
psql -h localhost -d userstoreis -U admin -p 5432 -a -q -f /home/jobs/Desktop/resources/postgresql.sql
-h PostgreSQL server IP address
-d database name
-U user name
-p port which PostgreSQL server is listening on
-f path to SQL script
-a all echo
-q quiet
Then you are prompted to enter the password of the user.
EDIT: updated based on the comment provided by @zwacky
I achived that wrote (located in the directory where my script is)
::someguy@host::$sudo -u user psql -d my_database -a -f file.sql
where -u user
is the role who owns the database where I want to execute the script then the psql
connects to the psql
console after that -d my_database
loads me in mydatabase
finally -a -f file.sql
where -a
echo all input from the script and -f
execute commands from file.sql
into mydatabase
, then exit.
I'm using: psql (PostgreSQL) 10.12 on (Ubuntu 10.12-0ubuntu0.18.04.1)
export PGPASSWORD=<password>
psql -h <host> -d <database> -U <user_name> -p <port> -a -w -f <file>.sql
You should do it like this:
\i path_to_sql_file
See:
You can give both user name and PASSSWORD on the command line itself.
psql "dbname='urDbName' user='yourUserName' password='yourPasswd' host='yourHost'" -f yourFileName.sql
Via the terminal log on to your database and try this:
database-# >@pathof_mysqlfile.sql
or
database-#>-i pathof_mysqlfile.sql
or
database-#>-c pathof_mysqlfile.sql