Test PostgreSQL API Beta
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
After creating a local cluster, follow the instructions below to test YugabyteDB’s PostgreSQL API.
psql is a command line shell for interacting with PostgreSQL. For ease of use, YugabyteDB ships with the 10.3 version of psql in its bin directory.
1. Connect with psql
- Run psql to connect to the service.
You can do this as shown below.
$ ./bin/psql --host localhost --port 5433 -U $USER
Database 'username' does not exist
psql (10.3, server 0.0.0)
Type "help" for help.
username=>
- Run psql to connect to the service.
You can do this as shown below.
$ ./bin/psql --host localhost --port 5433 -U $USER
Database 'username' does not exist
psql (10.3, server 0.0.0)
Type "help" for help.
username=>
- Install the psql client inside docker
$ docker exec -it yb-tserver-n3 yum install postgresql -y
- Run psql to connect to the service.
You can do this as shown below.
$ docker exec -it yb-tserver-n3 /usr/bin/psql --host yb-tserver-n3 --port 5433
Database 'username' does not exist
psql (10.3, server 0.0.0)
Type "help" for help.
username=>
- Install the psql client inside the container
$ kubectl exec -it yb-tserver-2 yum install postgresql
- Run psql to connect to the service.
$ kubectl exec -it yb-tserver-2 bash
$ psql --host localhost --port 5433
Database 'username' does not exist
psql (10.3, server 0.0.0)
Type "help" for help.
username=>
2. Create a table
Create a database called ‘sample’.
username=> CREATE DATABASE sample;
Connect to the database we just created.
username=> \c sample
psql (10.3, server 0.0.0)
You are now connected to database "sample" as user "username".
sample=>
Create a table named ‘stock_market’ which can store stock prices at various timestamps for different stock ticker symbols.
sample=> CREATE TABLE sample.stock_market (
stock_symbol text,
ts text,
current_price float,
PRIMARY KEY (stock_symbol, ts)
);
3. Insert data
Let us insert some data for a few stock symbols into our newly created ‘stock_market’ table. You can copy-paste these values directly into your cqlsh shell.
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('AAPL','2017-10-26 09:00:00',157.41);
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('AAPL','2017-10-26 10:00:00',157);
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('FB','2017-10-26 09:00:00',170.63);
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('FB','2017-10-26 10:00:00',170.1);
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('GOOG','2017-10-26 09:00:00',972.56);
INSERT INTO sample.stock_market (stock_symbol,ts,current_price) VALUES ('GOOG','2017-10-26 10:00:00',971.91);
4. Query the table
Query all the values we have inserted into the table.
sample=> SELECT * FROM sample.stock_market;
stock_symbol | ts | current_price
--------------+---------------------+---------------
AAPL | 2017-10-26 09:00:00 | 157.410004
AAPL | 2017-10-26 10:00:00 | 157.000000
FB | 2017-10-26 09:00:00 | 170.630005
FB | 2017-10-26 10:00:00 | 170.100006
GOOG | 2017-10-26 09:00:00 | 972.559998
GOOG | 2017-10-26 10:00:00 | 971.909973
(6 rows)