Connect to YSQL with YSQLSH and a JDBC URL

ysqlsh is the shell for interacting with the YugabyteDB YSQL API.

To connect to a database, you need the following information:

  • the name of your target database
  • the host name and port number of the server
  • the user name you want to connect as

You provide these parameters using the -d-h-p, and -U flags.

Example:

				
					[root@localhost ~]# yugabyted start --ysql_enable_auth=true > start.log

[root@localhost ~]# ysqlsh -h 127.0.0.1 -p 5433 -U yugabyte -d yugabyte
Password for user yugabyte:
ysqlsh (11.2-YB-2.19.2.0-b0)
Type "help" for help.

yugabyte=# ALTER USER yugabyte WITH PASSWORD 'some_password!';
ALTER ROLE

yugabyte=# CREATE DATABASE some_database;
CREATE DATABASE
				
			
Here’s another example, this time I am specifying the changed password of the yugabyte user via the PGPASSWORD environment variable…
				
					[root@localhost ~]# PGPASSWORD='some_password!' ysqlsh -h 127.0.0.1 -p 5433 -U yugabyte -d some_database
ysqlsh (11.2-YB-2.19.2.0-b0)
Type "help" for help.

some_database=#
				
			

We can also connect to a database using a JDBC URL instead of passing in all of those parameters to ysqlsh!

Example:

				
					[root@localhost ~]# ysqlsh postgres://yugabyte:some_password!@127.0.0.1:5433/some_database
-bash: !@127.0.0.1: event not found
				
			

Uh oh! That didn’t work. The problem is that exclamation point in the password.

Simply enclose the JDBC URL in sinlgle quotes to work around that issue!

				
					[root@localhost ~]# ysqlsh 'postgres://yugabyte:some_password!@127.0.0.1:5433/some_database'
ysqlsh (11.2-YB-2.19.2.0-b0)
Type "help" for help.

some_database=#
				
			

Have Fun!

Some cool SQL...