Run a PostgreSQL .sql file using command line arguments

前端 未结 12 1981
面向向阳花
面向向阳花 2020-11-28 00:08

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

12条回答
  •  独厮守ぢ
    2020-11-28 00:47

    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)

提交回复
热议问题