PostgreSQL Connecting to a Database: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 6: Line 6:


Assuming that a [[PostgreSQL_Concepts#Instance|PostgreSQL instance]] is online and available, a user must connect to a particular [[PostgreSQL_Concepts#Database|database]] hosted by the instance before performing data operations. The user establishes a [[PostgreSQL_Concepts#Session|session]], over a lower-level network or local socket [[PostgreSQL_Concepts#Connection|connection]], but in practice, the term sessions and connections are used interchangeably.
Assuming that a [[PostgreSQL_Concepts#Instance|PostgreSQL instance]] is online and available, a user must connect to a particular [[PostgreSQL_Concepts#Database|database]] hosted by the instance before performing data operations. The user establishes a [[PostgreSQL_Concepts#Session|session]], over a lower-level network or local socket [[PostgreSQL_Concepts#Connection|connection]], but in practice, the term sessions and connections are used interchangeably.
If the <code>psql</code> client and the PostgreSQL instance run on the same machine, the connection is established by default over a local socket, and the identity used to connect is given by the O/S user account.


=Connect=
=Connect=

Revision as of 22:23, 6 October 2023

Internal

Overview

Assuming that a PostgreSQL instance is online and available, a user must connect to a particular database hosted by the instance before performing data operations. The user establishes a session, over a lower-level network or local socket connection, but in practice, the term sessions and connections are used interchangeably.

If the psql client and the PostgreSQL instance run on the same machine, the connection is established by default over a local socket, and the identity used to connect is given by the O/S user account.

Connect

Connect to a database.


psql <database-name>

Options

-h|--host=

-U|--username=

-d|--dbname=

Specifies the name of the database to connect to.

-p

Specifies the port.

-c, --command

Run only single command (SQL or internal) and exit.

Examples

Connect Locally to the Default Database

psql postgres

This should work without any additional configuration.

Connect to a Remote Database

psql -h <host-name> -U <username> -d <database-name>
psql -h dev01.example.us-west-2.rds.amazonaws.com -U root -d dev01

If the database name is not specified, will connect to ...

Connect to the Default Database

psql -h localhost -U admin

Connect as a Specific User to a Database

psql -U <username> <dbname>

Connect to a PostgreSQL Instance Running in a Kubernetes Pod that Has Been Exposed as a NodePort Service

psql -h localhost -p 5432 -U postgres