18

In SQL Server, I use:

 use database_name
 Go
 Select * from table_name

In postgreSQL, I still use:

 \connect database_name
 Select * from table_name
========================
then error: 
ERROR:  syntax error at or near "\"
LINE 1: \connect database_name
        ^
********** Error **********

ERROR: syntax error at or near "\"
SQL state: 42601
Character: 1.

Why? Can do help me? Thanks.

2
  • Use PgAdmin - III call command. Commented Apr 28, 2017 at 2:55
  • 1
    You got errors because every command in psql CLI have to be terminated with ";" So, just do \connect database_name; And then, select * from table_name; Commented Apr 28, 2017 at 7:32

2 Answers 2

29

Technically PostgreSQL can't switch databases. You must disconnect and reconnect to the new DB.

The psql command-line client will do this for you with the \connect command, shortcut \c. But these are not commands processed by the PostgreSQL server, they're client commands. Different clients won't understand or support them.

At a guess you're using PgAdmin-III, in which case use the pulldown menu in the query tool to switch databases.

Some day I'd like to extract psql's backslash-commands code into a library that things like PgAdmin could link to and use too.

Sign up to request clarification or add additional context in comments.

Comments

1

Just I am adding the screenshot below, if we want to see the Postgres databases connection in SQL developer this would be work, I know not relavent to the Question, but somehow it gives the clarity while connecting the SQL developer. While connecting the other database in the postgres, here we need to select the Oracle database, then only it will shows the available databases for the particular user.

now we can able to see the different databases for the user

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.