Thursday, May 29, 2014

Permission denied in PostgreSQL

You've granted CREATE, CONNECT, and TEMPORARY privileges on the database to myuser but you haven't granted SELECT and INSERT table privileges yet. You'll need something like:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
In addition you need privileges on sequences if you have any serial columns or other column defaults drawing from sequences. Generally, the USAGE privilege is be enough for INSERT operations to work, but since you asked to "give all rights":
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
 
You will probably want to grant DEFAULT PRIVILEGES, too. So your administrator can access future objects automatically, too.
Can be done per schema:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO administrator; If you omit the schema, it applies to the whole database:
ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO administrator; Only applies to objects created by the role specified (defaulting to the role that executes this command):
ALTER DEFAULT PRIVILEGES FOR staff GRANT IN SCHEMA public ... ; Available since PostgreSQL 9.0.
Don't forget to GRANT privileges on SEQUENCES in addition if you have any. (For instance as source for default values in a serial column.)
 

ERROR: permission denied for sequence using PostgreSQL

Since PostgreSQL 8.2 you have to use:
GRANT USAGE, SELECT ON SEQUENCE cities_id_seq TO user;
GRANT USAGE - For sequences, this privilege allows the use of the currval and nextval functions.

Thursday, May 22, 2014

Granting access to all tables for a user on PostgreSQL Database

First, you have to be able to connect to the database in order to run queries. This can be achieved by
REVOKE CONNECT ON DATABASE your_database FROM PUBLIC;

GRANT CONNECT
ON DATABASE database_name 
TO user_name;
The REVOKE is necessary because
The key word PUBLIC indicates that the privileges are to be granted to all roles, including those that might be created later. PUBLIC can be thought of as an implicitly defined group that always includes all roles. Any particular role will have the sum of privileges granted directly to it, privileges granted to any role it is presently a member of, and privileges granted to PUBLIC.
If you really want to restrict your user to DML statements, then you have a little more to do:
REVOKE ALL
ON ALL TABLES IN SCHEMA public 
FROM PUBLIC;

GRANT SELECT, INSERT, UPDATE, DELETE
ON ALL TABLES IN SCHEMA public 
TO user_name;
These assume that you will have only one schema (which is named 'public' by default).

Wednesday, May 21, 2014

Configuring MS SQL Server for Remote Access

Problem:

A Microsoft SQL instance cannot be accessed remotely through ODBC, Visual Studio, or SQL Server Management Studio connection.

Resolution:

(applies to MS SQL 2005, 2008, 2008 R2, and 2012)
The Windows firewall is usually the culprit in these scenarios. Open TCP port 1433 for the service itself, and 1434 if you need to use the SQL Browser service. Read this article to learn how to Open an Inbound Custom Allow Rule in Windows Firewall.
  1. Open cliconfg from a RUN prompt and make sure TCP/IP is an enabled protocol. For SQL 2005/2008/2008 R2: Check the Services tool, Start > Administrative Tools > Services, to see that the service named SQL Server (MSSQLSERVER) is started.
    For MS SQL 2012: Use the Windows key or hover over the left lower corner of the desktop and select Administrative Tools, then Services to see that the service named SQL Server (MSSQLSERVER) is started.
  2. Ensure that you are using the correct credentials to authenticate. The default SQL administrator account is named sa and if you built the server from one of our server images with MSSQL pre-installed, the password will be in a text file on the root of the C partition.
  3. Use netstat –an from the command prompt to verify that the server is listening for SQL traffic on the correct ports.
  4. If the server is not listening for SQL traffic on the correct ports, use SQL Server Configuration Manager to change the ports.
    • For MS SQL 2005/2008/2008 R2, go to Start > All Programs > Microsoft SQL Server 2005 (or 2008/2008 R2) > Configuration Tools > SQL Server Configuration Manager. For MS SQL 2012: Use the Windows key or hover over the left lower corner of the desktop and select All Programs > Microsoft SQL Server 2012 > Configuration Tools > SQL Server Configuration Manager.
    • Open the + next to SQL Server Network Configuration.
    • Right-click TCP/IP and select Properties.
    • Select IP Addresses.
    • All TCP ports mentioned on all interfaces should be 1433. Change this to reflect the correct port number and restart the SQL services.
  5. If you are using named instances when installing SQL,  giving you the ability to host multiple SQL versions or service types, you will have to specify the name of the SQL instance when connecting rather than just using the server’s name or IP.  If you have created a named instance, you will need to access it by appending the name to the server’s name or IP, following a backslash (e.g. 12.34.56.78\SQLINSTANCENAME or SQLSERVERNAME\SQLINSTANCENAME).