Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In pg_restore, how can you use a postgres connection string to specify the host/database/username/password?

When using pg_dump, you can use a postgres connection string to specify the host/database/username/password:

pg_dump postgres://someuser:[email protected]:5432/somedatabase 

I want to use the same sort of connection string for pg_restore:

pg_restore -f dump.dump postgres://userb:[email protected]:5432/otherdatabase 

But I get an error:

pg_restore: [archiver] could not open input file "postgres://userb:[email protected]:5432/otherdatabase": No such file or directory 
like image 715
Oved D Avatar asked Feb 04 '15 15:02

Oved D


People also ask

What is the connection string for PostgreSQL?

Driver={PostgreSQL ANSI};Server=IP address;Port=5432;Database=myDataBase;Uid=myUsername;Pwd=myPassword;sslmode=require; Please note that sslmode=require is case sensitive, it should be written in lower case letters.

How do I connect to PostgreSQL database host?

1) Connect to PostgreSQL database server using psql First, launch the psql program and connect to the PostgreSQL Database Server using the postgres user: Second, enter all the information such as Server, Database, Port, Username, and Password.

What is Pg_restore in postgres?

Description. pg_restore is a utility for restoring a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats. It will issue the commands necessary to reconstruct the database to the state it was in at the time it was saved.


1 Answers

In PostgreSQL tools wherever you can specify a database name you can instead specify a connection string.

In the syntax for pg_restore the dbname is passed with a flag, not as a positional parameter:

$ pg_restore --help pg_restore restores a PostgreSQL database from an archive created by pg_dump.  Usage:   pg_restore [OPTION]... [FILE]  General options:   -d, --dbname=NAME        connect to database name   ... 

so you should be using:

pg_restore -d 'postgres://userb:[email protected]:5432/otherdatabase' dump.dump 

Yes, that user interface mismatch between pg_dump and pg_restore sucks, and I wish we could change it, but it's a bit late now.

like image 134
Craig Ringer Avatar answered Nov 10 '22 01:11

Craig Ringer