Run a PostgreSQL .sql file using command line arguments

前端 未结 12 1945
面向向阳花
面向向阳花 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:40

    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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-11-28 00:48
    export PGPASSWORD=<password>
    psql -h <host> -d <database> -U <user_name> -p <port> -a -w -f <file>.sql
    
    0 讨论(0)
  • 2020-11-28 00:49

    You should do it like this:

    \i path_to_sql_file
    

    See:

    Enter image description here

    0 讨论(0)
  • 2020-11-28 00:50

    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
    
    0 讨论(0)
  • 2020-11-28 00:53

    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
    
    0 讨论(0)
提交回复
热议问题