Run a PostgreSQL .sql file using command line arguments

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

    Walk through on how to run an SQL on the command line for PostgreSQL in Linux:

    Open a terminal and make sure you can run the psql command:

    psql --version
    which psql
    

    Mine is version 9.1.6 located in /bin/psql.

    Create a plain textfile called mysqlfile.sql

    Edit that file, put a single line in there:

    select * from mytable;
    

    Run this command on commandline (substituting your username and the name of your database for pgadmin and kurz_prod):

    psql -U pgadmin -d kurz_prod -a -f mysqlfile.sql
    

    The following is the result I get on the terminal (I am not prompted for a password):

    select * from mytable;
    
    test1
    --------
    hi
    me too
    
    (2 rows)
    

提交回复
热议问题