Reputation: 1515
I want to connect to my newly created database called "test" in psql, I have seen a command like this:
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "postgres")
What does that mean?
I tried to write:
\c {test|- postgres|- localhost|- 5432|- | conninfo}
but got error:
invalid integer value "5432|-" for connection option "port"
What should I do?
Upvotes: 1
Views: 5784
Reputation: 911
My PostgreSQL version is - psql (15.2 (Ubuntu 15.2-1.pgdg22.04+1))
Connect (\c) is used at psql command prompt and the command syntax is -
\connect (or \c) [ dbname [ username ] [ host ] [ port ] ]
this command takes the value from previous connection for all four input values if we don't pass any value. we can understand this as below -
postgres=# \c (if we don't pass anything on this prompt then it will take all values (db name, user name, host and port) from previous connection.)
postgres=# \c test (here we are passing one value and this value be taken as database name, for other values user name, host name and port will be used from previous connection)
postgres=# \c test testuser (here host and port will be inherit from previous connection while first value will be taken as database name while second value will be taken as user name)
postgres=# \c test testuser localhost (here port will be used from previous connection while other three values db name, user name and host will be taken from given three values)
postgres=# \c test testuser localhost 5432 (in this command all four values are passed hence it will pick from here only)
post execute above command the output is -
postgres=# \c testdb testadmin localhost 5432
Password for user testadmin:
You are now connected to database "testdb" as user "testadmin".
Upvotes: 0
Reputation:
the \c is used when you are already connected to a database and wants to connect to another database.
e.g if you are connected with postgres db and wants to connect with test database then you can do is \c test
and if using psql then
./psql -U postgres -d test -p 5432
Upvotes: 1
Reputation: 1515
And I just find that this way will work, for all other people like me:
\l
so that you will see a list of dbs that you created, and if you want to see a certain table then
\c db_name
Then you will successfully connect it if the db exists.
Upvotes: 0