Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to PostgreSQL without specifying a database name?

Tags:

php

postgresql

I need to connect to some PostgreSQL server providing some credentials, and print a list of available databases on that host for a given user.

I am trying:

<?php     $connection = pg_connect("host=localhost user=testuser password=123 connect_timeout=5"); ?> 

And I get:

Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: database "testuser" does not exist in /var/www/test.php on line 56 

I thought this must be possible because phpPgAdmin does it, but I looked at phpPpAdmin sources and found that they connect to a database named template1.

From http://www.postgresql.org/docs/8.1/interactive/manage-ag-templatedbs.html:

CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/pgSQL in template1, it will automatically be available in user databases without any extra action being taken when those databases are made.

Is there a way to connect without specifying any database?

like image 835
Silver Light Avatar asked Dec 19 '10 13:12

Silver Light


People also ask

How do I find my PostgreSQL database name?

Step 1: Log in to the server using the SQL Shell (psql) app. Step 2: Run the following query: SELECT datname FROM pg_database; psql runs the query against the server and displays a list of existing databases in the output.

How do I connect to PostgreSQL connection?

The default username for postgres is postgres. (If you are using Advanced Server it is enterprisedb.) On a Mac or Windows, you are able to connect to the default instance by simply hitting enter at the shell or command prompt when trying to run psql and keying in the password.

How to connect to the PostgreSQL database server?

The following steps show you how to connect to the PostgreSQL database server via the psql program: First, launch the psql program and connect to the PostgreSQL Database Server using the postgres user: Second, enter all the information such as Server, Database, Port, Username, and Password.

How do I create a user database in PostgreSQL?

use sudo -i -u postgres command to connect with the postgres user in PostgreSQL. type psql to start the PostgreSQL terminal, once this window is open we can start typing queries. Next step in the process is to create a database. We have created a database with the name ‘ users ‘.

How to use PostgreSQL in Linux?

Type psql on the terminal, you will see postgres=#, Now you can start typing the queries. You can not to the database directly by passing psql command after the user account. In the below syntax, Postgres is the name of the user account. The first query we are going to pass is to create a new database and then we’ll connect to that database.

How many databases are there in PostgreSQL?

Postgres by default has 3 databases, out of them one is with the name ‘Postgres’, and the user account is also created with the same name. First, we’ll show you how to connect with a user account in PostgreSQL.


1 Answers

You have to connect to a database. Which database you could use for a "maintenance database" depends on the installation and the subsequent administration. After a default installation there are 2 databases that could be used for the initial connection - "template1" and "postgres". It's wise to create a new user and a database with the same name and use those.

like image 152
Milen A. Radev Avatar answered Sep 24 '22 07:09

Milen A. Radev