如何使用命令行和URI将一个SQL文件导入到PostgreSQL数据库中?

huangapple go评论66阅读模式
英文:

How can I put an sql file into a postgresql database with a command line using URI?

问题

I've wrote an sql file with some inserts I want to do in my database.

Here's an example of what it's made of :

-- User
INSERT INTO users (user_id, credit, email, passwd, alias_login, user_id) 
VALUES (1, 250, 'test@test.com', '123456', '1111', 'admin');

And I've also wrote a batch script which install my database before and I want after the installation to put some datas inside it with this sql script inside the batch script.

I've already explore all the options and to be fully automatised I need to connect to my db with a string to avoid password typing like that :

C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"

But the thing that I want is to input to this my sql script considering that with this string method I can't use the -f option or any -c "\i ..." thing. It just ignores the command.

Also, I've tried this which didn't work at all :

type %cd%/myscript.sql | C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"
英文:

I've wrote an sql file with some inserts I want to do in my database.

Here's an example of what it's made of :

-- User
INSERT INTO users (user_id, credit, email, passwd, alias_login, user_id) 
VALUES (1, 250, 'test@test.com', '123456', '1111', 'admin');

And I've also wrote a batch script which install my database before and I want after the installation to put some datas inside it with this sql script inside the batch script.

I've already explore all the options and to be fully automatised I need to connect to my db with a string to avoid password typing like that :

C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"

But the thing that I want is to input to this my sql script considering that with this string method I can't use the -f option or any -c "\i ..." thing. It just ignores the command.

Also, I've tried this which didn't work at all :

type %cd%/myscript.sql | C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"

答案1

得分: 1

psql -d "postgresql://user:password@host/dbname" -c "SELECT 42"
 ?column? 
══════════
       42
(1 row)
英文:

<code>psql -c &lt;command&gt;</code> works just fine with a connection string URL:

psql -d &quot;postgresql://user:password@host/dbname&quot; -c &quot;SELECT 42&quot;

 ?column? 
══════════
       42
(1 row)

答案2

得分: 0

你可以使用PGPASSWORD环境变量或者使用.pgpass文件(在Windows上是pgpass.conf)。

使用PGPASSWORD

  • 在Linux上,你可以这样使用:
PGPASSWORD=mypassword psql -U myuser -f myscript.sql mydb

注意:你可能需要采取措施,以防它留在你的Bash历史记录中

  • 在Windows上(假设是cmd.exe):
SET PGPASSWORD=mypassword
C:\PROGRA~1\PostgreSQL5.2\bin\psql.exe -U myuser -f myscript.sql mydb

使用pgpass

在Linux上,该文件应该是$HOME/.pgpass。在Windows上,它是%APPDATA%\postgresql\pgpass.conf

这是一个以纯文本格式的文件:

hostname:port:database:username:password

例如:

localhost:5432:mydb:myuser:mypassword
英文:

You could either use the PGPASSWORD environment variable or use a .pgpass file (pgpass.conf on Windows).

Using PGPASSWORD

  • On Linux, you could just use:
PGPASSWORD=mypassword psql -U myuser -f myscript.sql mydb

Note: you may want to take measures so that it doesn't stay in your Bash history.

  • On Windows (assuming cmd.exe):
SET PGPASSWORD=mypassword
C:\PROGRA~1\PostgreSQL5.2\bin\psql.exe -U myuser -f myscript.sql mydb

Using pgpass

On Linux, the file should be $HOME/.pgpass. On Windows, it's %APPDATA%\postgresql\pgpass.conf.

This is a plain text file with this format:

hostname:port:database:username:password

For example:

localhost:5432:mydb:myuser:mypassword

huangapple
  • 本文由 发表于 2023年6月5日 23:58:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定