Run a PostgreSQL .sql file using command line arguments

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

    You have four choices to supply a password:

    1. Set the PGPASSWORD environment variable. For details see the manual:
      http://www.postgresql.org/docs/current/static/libpq-envars.html
    2. Use a .pgpass file to store the password. For details see the manual:
      http://www.postgresql.org/docs/current/static/libpq-pgpass.html
    3. Use "trust authentication" for that specific user: http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST
    4. Since PostgreSQL 9.1 you can also use a connection string:
      https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-28 00:32

    If you are logged in into psql on the Linux shell the command is:

    \i fileName.sql
    

    for an absolute path and

    \ir filename.sql
    

    for the relative path from where you have called psql.

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

    you could even do it in this way:

    sudo -u postgres psql -d myDataBase -a -f myInsertFile
    

    If you have sudo access on machine and it's not recommended for production scripts just for test on your own machine it's the easiest way.

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

    You can open a command prompt and run as administrator. Then type

    ../bin>psql -f c:/...-h localhost -p 5432 -d databasename -U "postgres"
    

    Password for user postgres: will show up.

    Type your password and enter. I couldn't see the password what I was typing, but this time when I press enter it worked. Actually I was loading data into the database.

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

    Of course, you will get a fatal error for authenticating, because you do not include a user name...

    Try this one, it is OK for me :)

    psql -U username -d myDataBase -a -f myInsertFile
    

    If the database is remote, use the same command with host

    psql -h host -U username -d myDataBase -a -f myInsertFile
    
    0 讨论(0)
提交回复
热议问题